2
\$\begingroup\$

I've been wearing my enter key down on google searches - I have a camera script based on the MouseOrbit.js asset. That's all working fine, but in addition to the basic orbiting and the zooming that I've added, I would like to use the WASD keys to move the camera around the world.

The W key would move the camera straight forward, however it would ignore the y axis. For example, using

transform.Translate(Vector3.Forward*Time.Delta*20);

moves the camera forward relative to the camera. This results in you quickly hitting the ground. Moving back obviously does the opposite. The desired effect is sliding across the world without getting any closer/farther to it, regardless of the angle the camera is at.

The closest I can get is using the Space.World parameter of Translate(), but this does not take into account the rotation of my camera. I think if I could take that into account, this would be solved but I'm not clear on how to do that.

\$\endgroup\$
1

1 Answer 1

3
\$\begingroup\$

You don't need to use transform.Translate. Just calculate how much the camera should move forward for a given frame (something like if the W key is held down do deltaPos = transform.forward * Time.deltaTime * 20), set the Y value of that Vector3 to zero, then add that delta vector to the original position by adding it to the current position transform.position += deltaPos;

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ah, transform.forward was the missing piece. Can't believe I didn't see that. Now I have to wait 7 hours to get home and try it out. Thanks Tetrad. \$\endgroup\$
    – Dakine83
    Feb 1, 2013 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .