How to make a hovering ball


I would like make a ball hover but move slight movement for an effect. Or make it rotate randomly.

Thanks, Chris

Here: Try this script.

var x = Random.Range(-10,10);
var y = Random.Range(-10,10); 
var z = Random.Range(-10,10); 

function FixedUpdate () {
    transform.Rotate(x, y, z);
    transform.Translate (1,1,1);

Put it on the object that you wish to see rotate. Also, to make it float, either don't add a rigidbody at all, or if you want to use one, turn off gravity for it.

I used this for a hovering object of my own.


    //the ball layer to ignore.  you could simply use the ignoreraycast layer on the ball as well and eliminate this part.
    private LayerMask layerMask = 1 << 8;
    //if you do, use the ~ operator to make this layer be the layer to ignore.
    void Start()
    layerMask = ~layerMask;
    void FixedUpdate()
    //put the direction in world space downwards.
    Vector3 rayDir = transform.TransformDirection(Vector3.down);
    RaycastHit hitInfo;
    if (Physics.Raycast(transform.position, rayDir, out hitInfo, Mathf.Infinity, layerMask))
    //if the ray hits any collider.
    if (hitInfo.transform.collider)
    //find the distance from the ray's contact point with the collider
    float distFromGround = hitInfo.distance;

//if the ray's distance has dropped below the desired hover distance put a relative force pushing the ball up.
    if (distFromGround < hoverDistance)
    rigidbody.AddRelativeForce(Vector3.up * hoverSpeed);
    //else the ball is more than that distance and so bring it back down again.
    rigidbody.AddRelativeForce(Vector3.down * gravity);

Hope this helps.