Hi, I would appreciate some suggestions for the following problem;
I’ve got a character with two basic animations, Walk and Idle. During Walk animation you can see the side of the character, while during Idle animation the character is facing the camera.
The flip method functions correctly while walking. However, it somewhat disfigures the character during idle animation when the scale is set to -1. I’d like to lock the character’s x scale to +1 when he’s idling.
My solution was to add the following animation behavior to the Idle animation.
public class KeepScaleBehaviour : StateMachineBehaviour {
private Transform playerTransform;
private GameObject player;
float updatedXscale;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.FindWithTag("Player");
playerTransform = player.GetComponent<Transform>();
if (playerTransform.localScale.x > 0)
return;
else
playerTransform.localScale = new Vector3(playerTransform.localScale.x * -1, playerTransform.localScale.y, playerTransform.localScale.z);
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (Input.GetAxis("Horizontal") > 0)
updatedXscale = 1;
else
updatedXscale = -1;
playerTransform.localScale = new Vector3(updatedXscale, playerTransform.localScale.y, playerTransform.localScale.z);
}
}
This behavior script seems to be doing the job, but I have a feeling that there’s a simpler or more efficient way of solving this issue.
Feel free to share any ideas. Thanks