- Home /
car moving sideways
when i try out the car i just added a script to it only moves sideways for some reason, i have copy and pasted the code below.
var forwardSpeed : float = 3; var turnSpeed: float = 2;
function Update ()
{
//this is the forward speed that will actually happen
var forwardMoveAmount = Input.GetAxis("Vertical")*forwardSpeed;
//actual turn amount
var turnAmount = Input.GetAxis("Horizontal")*turnSpeed;
//rotate the vehicle
transform.Rotate(0,turnAmount,0);
//push the vehicle forward with a force
rigidbody.AddRelativeForce(-forwardMoveAmount,0,0);
}
Answer by Owen-Reynolds · Oct 28, 2012 at 02:32 PM
The line `AddRelativeForce(-forwardMoveAmount,0,0)` reads inputs as the x/y/z push force. So it's currently pushing your car along negative X. Nothing wrong with that, and if your car is facing that way, it will go forwards. Just figure out which way your car is really facing (look at the model, whichever arrow is aimed out the front) and push that way.
Unity officially thinks that +Z is forward (the blue arrow.) So might be easier later if you aim the car that way and push on `(0,0,forwardMoveAmt)`.
thanks for the answer i tried out the code you posted and it made the car go backwards,then i put a $$anonymous$$us next to forward$$anonymous$$oveAmount and it made it go forward.I really appreciate for the help.
So your car is officially facing backwards (blue arrow points to trunk) and you're pushing it backwards, so everything is in synch.
Your answer
Follow this Question
Related Questions
Drag Script Not Working 1 Answer
How to Implement System.StringBuilder into JavaScript? 2 Answers
Unity Car Tutorial 1 Answer
Is there a "library" of scripts? 1 Answer