Object Float Above Ground

So, I’ve checked out as many threads/videos. But many I still havn’t solved my issue.

What is the best way to make a object float, said 1.5m above the ground, plus going up small inclines etc.

I’ve using Ray-cast, but doesn’t really work very well… I’ve tried a “Configurable Spring” but on one model, it works great, but on Another it doesn’t really work very well.

Hope you can help. Thank you.

One way I’ve used for hovering is to raycast downwards to get the distance to contact.

If you choose a minimum and a maximum distance, then you can calculate an upwards force based on a function of the contact points position between those distances.

For instance (and this is just pseudocode, not compileable!):

float distance = RaycastDownwardsFromMe();  // you need to write this
float minDistance = 2.0f;
float maxDistance = 4.0f;
float maxForce = 25.0f;
float fractionalPosition = (maxDistance - distance) / (maxDistance - minDistance);
float force = fractionalPosition * maxForce;
gameObject.GetComponent<Rigidbody>().AddForce( Vector3.up * force);

Something like that will get you started in the hovering, then you can push it around laterally, play with the damping, etc.

You may need to use layers to keep your raycasting from colliding with colliders on your actual agent object.

Actually, here’s a little project that will get you going, based on the above code.

https://bitbucket.org/kurtdekker/hovering

You can either sync the git project (best!) or find the downloads tab and get a snapshot.

1 Like

This seems to be working quite well! Thank you very much!.

Much appreciated!

1 Like