A hovering affect

I searched around and only found C# codes, can anyone give me a hovering affect in java script (Note my object is above water). I just wan’t a script I can attach to an object that makes it bob sorta up and down gently like a hover craft.

how about some bouyancy? You might need to adjust it so your “ocean” level varies based on a raycast pointed at the ground…

http://forum.unity3d.com/threads/72974-Buoyancy-script

No not like that, I mean something like a hover car where it is above the ground.

You can multiply the vertical position by sin, though you should probably add a raycast to make sure that it never goes below the ground.

Alternately, you could create an animation set to loop of the bobbing motion you want and have it play automatically.

Or, you could have
transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position);

and add an offset, which would change a little each frame. i think that the easyst way would be to make an animation for it, thought, like Pinkhair was saying

Buoyancy is the answer to it… but not in the traditional sense. You create 4 empty objects positioned around the vehicle. Each frame you do a Raycast downward from each of those points. I would do the raycast to the length of the hover amount. Apply upward force to each point ( Unity - Scripting API: Rigidbody.AddForceAtPosition ) in the amount of…

rigidbody.mass * (rideheight - Vector3.Distance(point.position, hit.point))

I did something like this once before, but my example is pretty primitive; its pretty much what BMB just described except I’m only casting one ray from the object’s center:

var height: float = 5.0;
var forceMultiplier : float = 10.0;

function FixedUpdate(){

    var hit : RaycastHit;

    if(Physics.Raycast(transform.position, -transform.up, hit, height)){
        rigidbody.AddForce(transform.up*(forceMultiplier/(hit.distance/2)));
    }

}

BMB’s formula for the amount of force to apply is probably better than mine though, he’s a math nerd :slight_smile: mine was more the result of trial and error, but it works pretty decent.