I would like to create a game similar to F-Zero. In order to do this, I need my starship to "stick" to the track (hovering just a bit), even if the track is upside-down or twisted. I also need to constrain the physics and the controls only over its local X and Z axis, because the vertical one is not needed.
I was thinking to use a Raycast collider that goes down the Y axis of the ship to inherit the track angulation, but my scripting abilities are really poor right now. Can anyone help me with a sample code, please? Thanks!
Here's a very simple version. First, set your vehicle's rigidbody to not be affected by gravity (it's a checkbox in the inspector).
Next, in your script, you'd probably want something like this in your FixedUpdate() which computes the ideal hover position, based on where the ray hits the surface. Now you have your ideal hover position, you need to apply a force to move your ship towards it. The script would probably look something like this (sorry, don't have time to test now):
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -transform.up, hit, 100.0)) {
idealPosition = transform.position + ((hoverHeight-hit.distance) * transform.up);
}
var pull : Vector3 = (idealPosition - transform.position);
rigidbody.AddForce( pull );