camera not moving forward as expected

I’m trying to create a simple camera control and I’m failing at the first step, moving the camera forward. When the scene starts I’m using lookat to point the camera at an object.

transform.LookAt(battlefield.transform.position);

Then I have the following code which fires when “W” is pressed:

transform.Translate(transform.forward*Time.deltaTime*cameraMoveSpeed);

My expectation is that the camera will move from its current position toward where it is looking. Instead the camera appears to move down and to the right.

What am I doing wrong?

That's difficult to say. What you've told us here SHOULD work, in theory. Can we see the snippet of code in which this is called?

1 Answer

1

Try to use Vector3.forward instead of transform.forward in the second line. :slight_smile:

Also maybe try resetting the forward position of the object. Or maybe it is translating it in the local space. These are only suggestions

Vector3.forward worked, thank you for the help. If you don't mind can you either explain or link me to some docs so I can learn why what I originally had was wrong?

http://docs.unity3d.com/Documentation/ScriptReference/Vector3-forward.html http://docs.unity3d.com/Documentation/ScriptReference/Transform-forward.html I hope this helps

It helps a lot thank you. Now I know the camera always faces the transfrom Z. And transform.forward returns the vector in world space where the transform is facing. And Translate by default moves relative to local directions. So what was happening was was the vector I was moving on was relative to my local space but the coordinates came from the forward facing in world space.