x


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!

more ▼

asked Jan 27 '10 at 07:13 PM

DanjelRicci gravatar image

DanjelRicci
380 17 20 31

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 );
more ▼

answered Jan 27 '10 at 09:17 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

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' ?"

Jan 27 '10 at 10:48 PM DanjelRicci

tweaked it a bit. Yes I meant "position" instead of "Position"! still not in front of Unity so I can't test again, but see how it goes.

Jan 28 '10 at 07:27 AM duck ♦♦

Ok, thanks! I'm going to try it today. Precisely, I need to allign the vehicle to the triangle detected by the Raycast.

Feb 01 '10 at 10:23 AM DanjelRicci

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!

Feb 01 '10 at 07:37 PM DanjelRicci

have you tried increasing the amount of force applied? eg: rigidbody.AddForce( pull * someFactor );

Feb 01 '10 at 08:16 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1947
x1578
x102

asked: Jan 27 '10 at 07:13 PM

Seen: 4610 times

Last Updated: Jan 27 '10 at 07:13 PM