Rotating sprite rotates the controls
Hello i want to rotate the sprite 90 degrees. i use this to rotate:
transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
and this is my movement code:
void Update () {
hor = (int)Input.GetAxisRaw ("Horizontal");
if (hor!=0){
Move(hor,0);
} else {
Move(0,(int)Input.GetAxisRaw ("Vertical"));
}
}
private void Move(int h, int v){
//h ~[-1;1] ;; v ~ [-1;1]
ve = new Vector2 (h, v);
transform.Translate (ve * speed * Time.deltaTime);
}
So when the sprite is not rotated everything is fine, character moves as expected. But when i rotate the sprite, controlls rotate too. pressing W makes the character move left(the way sprite is facing) pressing A moves down etc.
So two questions: 1. Why is this happening? 2. How do i fix this?
Answer by hexagonius · Sep 06, 2015 at 08:44 PM
you're basically altering the alignment of the axis relative to the world coordinate system. so right becomes up, up left, left down, down, right. watch the translate gizmo in the scene view while playing to see the changes.
How to fix: make the sprite a child of the player. move the player but rotate the child (Sprite). this way your sprite rotates freely but the player stays correctly aligned.
Your answer