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?).

1 Answer

1

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.