Just wondering how you guys set up the vehicle physics in Unity, did you use raycasting and spring joints?
Just wondering how you guys set up the vehicle physics in Unity, did you use raycasting and spring joints?
Simpler than springs. Just basically apply Newton's laws of motion.
You know that for an object to stay in rest no forces can be applied on it: Fr=0 (being Fr the sum of all forces applied on the object).
So here you have the gravity pulling down on the ship so you create a force that counters that gravitic pull, making your ship still in mid-air. Then you make that levitating force vary with the inverse of the ship's height above the ground (F=k/h in which F is the force that counters the gravity pull, k is a constant that works like a "bounce" factor and h is the raycast distance between the ship's geometric center and the track, measured perpendicular to the track's polygons) so that the ship maintains it's height after jumps and bumps.
Last edited by Xpand; 3rd January 2013 at 01:07 PM.
Thanks for the reply.
Just trying to understand it.
I've set the Rigidbody of the ship to 1000 and added a raycast downwards that stores the distance between the center of the ship & the track. I then have a bounce rate float of 600000 that is divided between the raycast distance, the results are then added as downward force from the ship.
This gives a hover effect but doesn't seem to spring to a height above the track. I'm doing something wrong I imagine, but I don't know what.
I guess you need some adjustments on having the floating force variate with the height. Does your ship drop below/ fly away from the normal floating height after jumps or bumps?
It doesn't follow the slope of the track (just a plane with rotation on it for now) it just goes forwards and crashes in to the track.![]()
Are you sure you're measuring the raycast between the ship and the track? Because the force alone would be enough to keep your ship at the same height in every point of the track.
Try to normalize the raycast value by dividing it by a set height value.
For example:
Lets use the F=k/h formula:
You want the ship to stay afloat at a height of 2 units, then you divide the raycast value (also measured in units) by those 2 units (h=raycast/2) so that the value of h, when the ship is at 2 units of height, is 1.
When the h is 1 you want the ship to stay still, so you set the "k" to exactly the same value as the gravity pull force only with the opposite direction.
Now you have a resultant force of 0 force units, which means that the ship has no acceleration on the vertical axis, so it still in that axis. It's called equilibrium.
If the raycast value drops below 2 (when crossing bumps and ramps), the h becomes smaller than 1 and that increases the value of the lift force, since dividing a constant (k) by a number smaller than one will output a number larger than k. That increases the lift force and makes the ship accelerate upwards.
If the raycast value is greater than 2 (i.e. after a jump), the outcome is the exact opposite of the last one and the ship suffers the effect of the gravitic pull.
Last edited by Xpand; 3rd January 2013 at 04:10 PM.
Thanks for the explanation, do I use an if statement for checking that h is 1?
Also, what rigidbody value should I use?
This system is totally analogic. You don't need logic tests like an if to make this work.
The expression itself will adjust autonomously to the value of "h".
well, if with rigidbody value you mean mass, I would start with 1 and then increase or decrease it until it looks right...
I think the only thing it will affect is the floating height (the heavier the ship is, the lower it will float) and reaction speed (the lighter it is the faster it reacts to track irregularities).