Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

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

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

    
I have suggested that a moderator migrate this to Game Development. –  Pieter Geerkens Mar 23 '13 at 9:18

2 Answers 2

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

share|improve this answer

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);
share|improve this answer
2  
you don't need to switch direction after each rotation. Consider setting Space.World parameter –  Ilya Ivanov Mar 23 '13 at 9:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.