- bool moveLeft = Input.GetKey(KeyCode.LeftArrow);
- bool moveRight = Input.GetKey(KeyCode.RightArrow);
- bool moveUp = Input.GetKey(KeyCode.UpArrow);
- bool moveDown = Input.GetKey(KeyCode.DownArrow);
- // Animation and position control
- bool isMoving = moveLeft || moveRight || moveUp || moveDown;
- playerAnimator.SetBool("isMoving", isMoving);
- if (moveRight)
- this.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
- } else if (moveLeft)
- this.transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
I'm using the code above to 'mirror' the 2D player character based on a keypress or more precisely the direction in which it moves. Is it bad practice or do I suffer some performance penalty if I set transform.localScale to a value that it currently has? If the player character keeps moving right the localState value will constantly stay the same.
I would hope that setting the values is separate from the rendering itself but I want to be sure that my solution is an acceptable one. Is there a better way to do this kind of thing? Does Unity have such a feature out of the box?