- Home /
Moving character on x axis
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?
Answer by whydoidoit · Mar 23, 2013 at 10:24 AM
Translate uses local coordinates unless you tell it to use world coordinates (which is why the rotation affects it).
void Update () {
transform.Translate(5f * Time.deltaTime, 0f, 0f, Space.World);
}