- Home /
Lock character's LocalScale during Idle animation
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
Answer by unity_oEoMzC5COK722Q · Dec 23, 2018 at 04:04 PM
I suggest you put in a check in the flip function of your character controller script—an if statement or a boolean—that checks whether your character's rigidbody.velocity is greater than zero, and if so then the flip function can be executed. I haven't tried it myself yet but I believe this is a viable option. The problem with your method is that firstly, it is costly to the performance if you have multiple character's with the same state machine behaviour (the calculation is made every-time character enters idle state) and secondly there's a lot of potential for unwanted behaviour, especially when you add more animation layers or clips.
Thanks for the answer. I actually did run into problems with my solution later on. I tried what u suggested. However, as soon as the character starts moving to the left, the scale is set to -1 again. So even if my X velocity reaches 0, the scale is still at -1 when I let go of the movement button and the idle animation starts playing. I think I might be able to elaborate on your idea a little bit. I also tried to set the scale to +1 when the velocity reaches 0 but that only inverts the flip functionality :D
Your answer
Follow this Question
Related Questions
Changing transform.localScale of a prefab in the script not working. Help pls! 1 Answer
Having trouble with flipping my projectile. 0 Answers
LocalScale not working idealy 1 Answer
How do I flip an enemy that's automatically walking and targeting the player? 1 Answer
2D Input.GetAxis not recognized when local scale is flipped 0 Answers