- Home /
Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not?
I have the following code:
var moveSpeed:float = 10.0; var rotateSpeed:float = 100.0;
function Update () { var translate:float = Input.GetAxis( "Vertical" ) moveSpeed; var rotate:float = Input.GetAxis( "Horizontal" ) rotateSpeed; Debug.Log( transform.forward ); //Debug.Log( rotate ); translate = Time.deltaTime; rotate = Time.deltaTime;
transform.Translate( 0, 0, translate );
transform.Rotate( 0, rotate, 0 );
}
Which functions as expected (WS control forward movement, AD control rotation). However, if I try the following:
var moveSpeed:float = 10.0; var rotateSpeed:float = 100.0;
function Update () { var translate:float = Input.GetAxis( "Vertical" ) moveSpeed; var rotate:float = Input.GetAxis( "Horizontal" ) rotateSpeed; Debug.Log( transform.forward ); //Debug.Log( rotate ); translate = Time.deltaTime; rotate = Time.deltaTime;
transform.Translate( transform.forward * translate ); // changed here!
transform.Rotate( 0, rotate, 0 );
}
It creates a disparity between the camera direction and the movement direction. Specifically, rotating the camera full circle seems to reverse the forward direction (so perhaps the scale is halved?).
Answer by Jessy · Jan 27, 2011 at 03:32 PM
First, your code doesn't reflect your question; the question asks about Rotate but your problem is with Translate; you should edit the question.
The problem lies in the Vector you're using. Your original code is equivalent to
transform.Translate(Vector3.forward * translate);
not
transform.Translate(transform.forward * translate);
http://answers.unity3d.com/questions/35551/simple-relativity-question
There are a whole bunch of overloaded methods for Transform.Translate. Pick one that offers the space you want to work in, and looks most readable given your code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Parenting without transform.rotation 1 Answer
Camera transform not responding after load 0 Answers
smooth fps movements using joystick 1 Answer
can anyone tell me why this doesn't work? 1 Answer
Aim Down Sights Positioning Problem 1 Answer