x


Airplane Script Control

I need help with my airplane script. my airplane wont move at all so can anyone see the problem with it

var throttle : int = 0;
var currentSpeed : float = 0.00;
var maxSpeed : float = 0.00;
var maxAltitude : float = 100.00;
var currentAltitude : float = 0.00;
var gravity : float = 200.0;

private var moveDirection : Vector3 = Vector3.zero;
var aircraftCtrl : CharacterController;

function Start(){
maxSpeed = calculateMaxSpeed(throttle);
}

function Update(){

currentAltitude = transform.position.y;

// Listen for input key entry
if(Input.GetButtonDown("ThrottleUp"))
{
    if(throttle < 10)
    {
        throttle++;
    }
    maxSpeed = calculateMaxSpeed(throttle);
}

if(Input.GetButtonDown("ThrottleDown"))
{
    if(throttle > 0)
    {
        throttle--;
    }
    maxSpeed = calculateMaxSpeed(throttle);
}

    if(currentSpeed < maxSpeed)
    {
        currentSpeed = currentSpeed + 0.05;
    }
    if(currentSpeed > maxSpeed)
    {
        currentSpeed = currentSpeed - 0.02;
    }

    if(currentSpeed > 40)
    {
        maxAltitude = 2 * (throttle * 10);
    }
    else
    {
        maxAltitude = 1.00;
    }
if(currentSpeed > 3 && currentSpeed < 40)
{
    moveDirection.y -= (gravity / 100) * (Time.deltaTime / 50);
    aircraftCtrl.Move(moveDirection * (Time.deltaTime / 30));
}

if(currentSpeed > 40 && currentAltitude <= maxAltitude)
{
    moveDirection.y = gravity * (Time.deltaTime / 5);
    aircraftCtrl.Move(moveDirection * (Time.deltaTime / 3));
}

Move function(){
    moveDirection.x = (currentSpeed * (Time.deltaTime)) * 3;
    aircraftCtrl.Move(moveDirection * Time.deltaTime);
}
    print("Throttle Position: " + throttle + " Current Speed: " + currentSpeed + 
    " Max Altitude: " + maxAltitude + " Altitude: " + currentAltitude + " Gravity: " + 
    moveDirection.y);
}

function calculateMaxSpeed(throttle)
{
    if(throttle == 0)
    {
        maxSpeed = 0.00;
    }
    else
    {
        maxSpeed = throttle * 10;
    }
    return maxSpeed;
}
more ▼

asked May 16 '10 at 03:13 PM

dontay gravatar image

dontay
79 4 5 7

If would be helpful if you formatted your code to read easier. Select all of it and hit the 1 & 0's button in the edit panel. It will shift the whole thing over 4 spaces.

May 16 '10 at 04:42 PM Peter G

Fixed the code formatting - most of it, anyway. :)

Jun 13 '10 at 05:47 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

A few ideas for you.

  1. I don't know if you dragged the character controller to the aircraftCtrl variable.

  2. I didn't see anything that called this function in your code:


    Move function(){ moveDirection.x = (currentSpeed * (Time.deltaTime)) * 3; aircraftCtrl.Move(moveDirection * Time.deltaTime); }


Controller.Move is calling its own version of the function so maybe you were trying to override it? I would appear that you are trying to cause the motion with this function, but you are never calling it. To call it, all's you have to do is say Move() instead of aircraftCtrl.Move().

This might be the most important piece. Character Controllers are not designed for planes so I would not recommend using one. A mesh collider with a rigidbody or box collider would give you more realistic results.

more ▼

answered May 16 '10 at 04:46 PM

Peter G gravatar image

Peter G
15k 16 44 136

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

To get your code to work you need to apply forces to your object. This is done via a manipulation to one or more rigid bodies.

There is a ton of resources on this in the forums. Just do a search for "airplane" or "Aircraft" or biplane.

http://forum.unity3d.com/viewtopic.php?t=52967&highlight=aircraft

more ▼

answered Jun 13 '10 at 04:24 PM

Mark Grob gravatar image

Mark Grob
12 1 1 1

(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:

x670
x342
x206
x68

asked: May 16 '10 at 03:13 PM

Seen: 6328 times

Last Updated: Jun 13 '10 at 05:46 PM