x


Unity car script problem

I am making a car game in Unity and I have created a JavaScript script that lets me move the car around. The scripts works fine unless the car goes over a bump or hill. It will then fly off into the air, regardless of weight and gravity.

I'm reasonably sure this is because transform.translate includes the rotation so if the car is looking up then it will move up because the z axis is pointing up.

The code for the car is:

var speed = 0.0;

var maxSpeed = 6; var minSpeed = -0.5;

var accelerationSpeed = 0.0;

var turnSpeed = 0.0;

var frontLeftWheel : Transform; var frontRightWheel : Transform; var steeringWheel : Transform; var maxSteerAngle = 30;

function Update () { transform.Translate (0, 0, speed); transform.Rotate (0, Input.GetAxis ("Horizontal") *turnSpeed, 0);

var rotation = Input.GetAxis ("Horizontal") * maxSteerAngle;
frontLeftWheel.localEulerAngles = Vector3 (0, rotation, 0);
frontRightWheel.localEulerAngles = Vector3 (0, rotation, 0);
steeringWheel.localEulerAngles = Vector3 (43.99999, 0, -rotation);


getMoveSpeed();
getTurnSpeed();

}

function getTurnSpeed() { turnSpeed = accelerationSpeed;

if (accelerationSpeed <= 0.1)
{
    turnSpeed = 0.1;
}
if (accelerationSpeed == 0)
{
    turnSpeed = 0;
}
if (accelerationSpeed < 0)
{
    turnSpeed = -1;
}

}

function getMoveSpeed() { speed = accelerationSpeed; if(Input.GetKey("up") & (speed < maxSpeed)) { accelerationSpeed += 0.008; }

if (!Input.GetKey("up") & (speed > 0))
{

    if  (accelerationSpeed > 0 & !Input.GetKey("down"))
    {       
        accelerationSpeed -= 0.004;
        if (accelerationSpeed < 0.004)
        {
            accelerationSpeed = 0;
        }
    }
}

if(Input.GetKey("down") & (speed > minSpeed))
{
    accelerationSpeed-= 0.008;
}

if (!Input.GetKey("down") & (speed < 0))
{

    if  (accelerationSpeed < 0 & !Input.GetKey("up"))
    {       
        accelerationSpeed += 0.004;
        if (accelerationSpeed > 0.004)
        {
            accelerationSpeed = 0;
        }
    }
}

if (Input.GetButton("Jump"))
{
    if (accelerationSpeed > 0)
    {
        accelerationSpeed -= 0.05;
    }
    if (accelerationSpeed < 0)
    {
        accelerationSpeed = 0.0;
    }
}

} I know its messy but I'm only year 10

If anyone has any ideas on what my problem could be or a better script could they please post it. Thanks

more ▼

asked Nov 05 '10 at 06:56 AM

thesquareof4 gravatar image

thesquareof4
17 3 3 3

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

4 answers: sort voted first

I think adding a rigid body would solve it, but Im not sure...

more ▼

answered Jun 21 '11 at 08:57 PM

kop4 gravatar image

kop4
44 4 4 7

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

Wow!! :) I tried your script but yes there is a problem. I also modified it that did not work :( So if i was you I will make another script. Sorry I cant help.

more ▼

answered Aug 22 '12 at 09:22 AM

hamzah gravatar image

hamzah
1

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

Aug 22 '12 at 09:23 AM alucardj
(comments are locked)
10|3000 characters needed characters left

Wow!! :) I tried your script but yes there is a problem. I also modified it that did not work :( So if i was you I will make another script. Sorry I cant help.

more ▼

answered Aug 22 '12 at 09:26 AM

hamzah gravatar image

hamzah
1

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

Also, until your karma rises, all questions and comments have to be approved by a moderator before it is published, so you'll have to wait until someone approves it before it can be seen.

Aug 22 '12 at 09:27 AM alucardj
(comments are locked)
10|3000 characters needed characters left

wow, you are a talented ten year old. Maybe try using the wheel colliers

more ▼

answered Jul 12 '12 at 03:01 AM

frankrs gravatar image

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

x1880
x1372
x418

asked: Nov 05 '10 at 06:56 AM

Seen: 2357 times

Last Updated: Aug 22 '12 at 09:27 AM