- Home /
Creating a magnetic vehicle (like F-Zero)
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!
Answer by duck · Jan 27, 2010 at 09:17 PM
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 );
Thanks for the reply! I tested it, but I got this errors:
1 - "Assets/Raycast Collider.js(6,40): BCE0051: Operator '-' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'."
2 - "Assets/Raycast Collider.js(9,49): BCE0019: 'Position' is not a member of 'UnityEngine.Transform'. Did you mean 'position' ?"
tweaked it a bit. Yes I meant "position" ins$$anonymous$$d of "Position"! still not in front of Unity so I can't test again, but see how it goes.
Ok, thanks! I'm going to try it today. Precisely, I need to allign the vehicle to the triangle detected by the Raycast.
I tried the script: unfortunately it doesn't help. :( The ship correctly hovers on the track at the right height, but it doesn't stick to it: where the track is wavy, the ship jumps away!
have you tried increasing the amount of force applied? eg: rigidbody.AddForce( pull * someFactor );
Your answer
Follow this Question
Related Questions
Raycast Car-Vehicle Physics 1 Answer
How do i make my Raycast Car not slip at all? 0 Answers
Raycast align with ground normal 2 Answers
Line of Sight with Physics.Linedraw or Physics.Raycast Unreliable? 1 Answer
Hovercraft Physics Problems 1 Answer