HELP - Vehicle controls for Sci-Fi Flying Cars?

Hello there,

I’ve been trying to make an open-world game where you are able to get behind the wheel of these ‘Fifth Element’-esque flying cars.

To give you an idea how the cars should look and react/manouver here’s a video from the movie “The Fifth Element”

I want these cars to fly smooth, feel heavy and it’s controls sophisiticated. Being able to make the car pitch up and down with the ‘UP’ and ‘DOWN’ arrow keys, make it roll sideways with ‘LEFT’ and ‘RIGHT’ arrowkeys. Accelerate with ‘W’ and brake with ‘S’.
Make the car turn (Giving it a fluid rolling effect too as it makes it’s turns) with ‘A’ and ‘D’.

This seems like one heck of a job to accomplish, and I just started out with Unity scripting. (Right now I’m working in Javascript, if there’s a better alternative I’d love to hear it.)

So far my controls are this little set of strings and code.

#pragma strict

var rotateSpeed = 20.0;
var speed = 10.0;


function Update() {
var transAmount = speed * Time.deltaTime;
var rotateAmount = rotateSpeed * Time.deltaTime;

if (Input.GetKey("down")) {
transform.Rotate(rotateAmount, 0, 0);

}
if (Input.GetKey("up")) {
transform.Rotate(-rotateAmount, 0, 0);
}
if (Input.GetKey("d")) {
transform.Rotate(0, -rotateAmount, 0);
}
if (Input.GetKey("a")) {
transform.Rotate(0, rotateAmount, 0);
}

if (Input.GetKey ("right")) {
transform.Rotate(0, 0, rotateAmount);
}

if (Input.GetKey ("left")) {
transform.Rotate(0, 0, -rotateAmount);
}

if (Input.GetKey ("s")) {
transform.Translate(0, 0, transAmount);
}

if (Input.GetKey ("w")) {
transform.Translate(0, 0,(transAmount * -2));
}

}

This is obviously very simple, and even though it does give a certain desired set of controls for my flying car, it’s very sharp. No smoothing whatsoever. For example; When you let go of the ‘gas’ you simply stop instantly.

But what I for example would like is for the car to have these ease in- and outs during movement.

I’ve been searching for a set of code to do the trick for a while now, but can’t really seem to get a hold on it.
So I’ll just ask it here myself; Any suggestions on what I might do? Change or create a completely different code?

Much appreciated! :slight_smile:

I’d consider using the physics engine for this. If you put a rigidbody on your car (turn off gravity), then in your FixedUpdate() you can call AddRelativeForce for the gas and brakes:

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddRelativeForce.html

And AddRelativeTorque for bank/pitch/yaw:

For your “turn” logic, you might apply some banking and yaw at the same time. You can play with the damping factor values to control how fast you slow down and stop banking, etc. Depending on how you want your game to work, you might also play with the idea of auto-leveling the car if there’s been no input for a while.

Thanks! I’ll definitely try that, sounds like exactly what I need.

Alright so I put in the AddRelativeForces like you suggested, and it works like a charm! After some tweaking with the setup (Altering the values of course. :P) I got the perfect result, just what I wanted! Thanks a bunch, Steve. :smile: