-1
\$\begingroup\$

I am making a basic movement script based off of the built in playercontroller in Unity. I am trying to get the Player gameobject to move forward based on its own rotation (i.e. if it is rotated to 90 degrees, it will go forward in the direction it is facing and not based off of its position values), but I can't figure out how to change it.

How can I do this?

EDIT: I figured it out. I just had to multiply the playercontroller's movement by player rotation.

\$\endgroup\$
2
  • \$\begingroup\$ How do you distinguish between "the direction it is facing" and "not based off of its transform values"? It's usually the transform values that determine which way an object is facing. If that's not the case for your situation, you may want to edit your question to show in detail how you're adjusting your object's facing direction at present. If you're not doing anything particularly special with facing, you should find lots of existing tutorials and Q&A here that talk about moving relative to an object's rotation/local coordinates. \$\endgroup\$ – DMGregory Jul 6 '20 at 11:39
  • \$\begingroup\$ If you've solved your problem, you should post your solution (with code) as an Answer, not as an edit to your Question. \$\endgroup\$ – DMGregory Jul 7 '20 at 17:21
1
\$\begingroup\$

I think what you need is the value transform.forward This is a normalized vector that will always point in the same direction as the object's blue arrow in the scene view. Feeding this into the character controller's Move method will move the object forward, regardless of rotation.

Do note that the character controller is only designed to function with an objective 'down' aligned to the y-axis. This means that if you rotate on any axis but the y-axis, it can lead to some strange results.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.