- Home /
Forward movement relative to rotation/position?
I'm making a space exploration game, it currently has 0 gravity applied to the entire scene. What I'm trying to get it to do is move in the direction it is facing, so all WASD does is control rotation. But with this current script the rotation works fine, but with a space click it moves up. Up and only up. Hope you can help, thanks.
var speed = 0.8;
var turnSpeed = 0.15;
function Start () {
rigidbody.useGravity = false;
}
function Update () {
if (Input.GetButton("Jump")) { //Spacebar by default will make it move forward
rigidbody.AddRelativeForce (Vector3.forward*speed);
}
rigidbody.AddRelativeTorque ((Input.GetAxis("Vertical"))*turnSpeed,0,0);
// W key or the up arrow to turn upwards, S or the down arrow to turn downwards.
rigidbody.AddRelativeTorque (0,(Input.GetAxis("Horizontal"))*turnSpeed,0);
// A or left arrow to turn left, D or right arrow to turn right.
}
Answer by roamcel · Oct 11, 2011 at 07:08 AM
If I understand correctly, your whole issue is related to world and local axes.
Relative torque needs a global vector, so if you apply the global 'forward' or 'up', they will ALWAYS be -world- forward and up, which is different from the local forward or up. Sadly, documentation is 100% obscure in regards to which are local and which are world references for these properties.
This should fix it:
Vector3 currentvertical = transform.TransformDirection(transform.up*Input.GetAxis("Vertical")*turnSpeed);
rigidbody.AddRelativeTorque(currentvertical);
Be sure to do the equivalent for horizontal (using transform.right), and you should be set.
For some reason, it keeps telling me to add a semicolon at line (3,8) and (4,8) Referring to these two lines?
Line 3
Vector3 currentvertical = transform.TransformDirection(transform.up*Input.GetAxis("Vertical")*turnSpeed);
Line 4
Vector3 currenthorizontal = transform.TransformDirection(transform.right*Input.GetAxis("Horizontal")*turnSpeed);
Your answer
Follow this Question
Related Questions
relativeVelocity how to? (noob question) 2 Answers
rigidbody2D force nesting (moving platform) 0 Answers
How do I spread a movement over several frames instead of one? 1 Answer
Add Relative Force 2 Answers