3
\$\begingroup\$

I am new to unity scripting.

I am trying to move my character towards right side(on x-axis) to create a running effect, I've imported the character from blender which contains two animations, "jump" and "run". What I want to do is I want my character to move on x-axis for which I've written following code

void Update () {
    transform.Translate(5f * Time.deltaTime, 0f, 0f);
}

But I am facing one problem, if I set my character's y-axis rotation to 90 degrees in order to make him look towards right side I am getting strange results. That is, in play mode instead of moving towards right side(on x-axis) it move on x-axis and z-axis simultaneously & if I set the y-axis rotation to zero it behaves like I want it to. Please see the attached image for detail. How can I solve this problem?

enter image description here

\$\endgroup\$

migrated from stackoverflow.com Mar 24 '13 at 13:02

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ I have suggested that a moderator migrate this to Game Development. \$\endgroup\$ – Pieter Geerkens Mar 23 '13 at 9:18
3
\$\begingroup\$

Use Translate overload, which takes Space enumeration values, indicating, that shift is an absolute value, like:

transform.Translate(5f * Time.deltaTime, 0f, 0f, Space.World);

or in more readable way

transform.Translate(Vector3.left * 5f * Time.deltaTime, Space.World);

Vector3.left returns a unit vector pointing left

\$\endgroup\$
1
\$\begingroup\$

I found the solution to my problem. And just like I thought, When Switching to my player model I had to change the movement direction from

transform.Translate(speed* Time.deltaTime,0,0);

to

transform.Translate(0,0,speed* Time.deltaTime);
\$\endgroup\$
  • 2
    \$\begingroup\$ you don't need to switch direction after each rotation. Consider setting Space.World parameter \$\endgroup\$ – Ilya Ivanov Mar 23 '13 at 9:28

Your Answer

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