- Home /
My Anima 2D's flipped animations are bugging out.
I'm not sure what it is and sometimes there isn't a problem at all. This is how it looks going right which is the default - in other words, without changing scale:
https://twitter.com/3lPsyCongroo/status/828843021986832384/photo/1
The rootSpeed variable in the animator is just a check to see what the root of the character is doing. The varying between 38.7 and 35.8 has to do with curves on the animation and isn't what I'm worried about.
Now this is moving left, with the scale inverted:
https://twitter.com/3lPsyCongroo/status/828849556573937670
As you can see, the rootSpeed value in the animator is twitching - it seems like it is constantly changing from positive to negative. You can briefly see it reverse directions when the velocity is 0.
Here's the controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MarionetteRootController : MonoBehaviour {
Animator anim;
Rigidbody2D rbody;
float rbodyX;
// Use this for initialization
void Awake ()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
Move();
}
void Update()
{
Turn();
}
void LateUpdate()
{
GetRootSpeed();
}
private void Move()
{
anim.SetFloat("hSpeed", Input.GetAxisRaw("Horizontal"));
}
private void Turn()
{
if (Input.GetAxisRaw("Horizontal") < 0)
{
gameObject.transform.localScale = new Vector3(-1, 1, 1);
anim.SetBool("facingRight", false);
}
else if (Input.GetAxisRaw("Horizontal") > 0)
{
gameObject.transform.localScale = new Vector3(1, 1, 1);
anim.SetBool("facingRight", true);
}
}
private void GetRootSpeed()
{
anim.SetFloat("rootSpeed", rbody.velocity.x);
}
}
I attempted to play around with the fixed update (before everything was in Update) but no difference. Here is the animator:
The transition from Idle->Walk ->Run are the values for hSpeed (getAxis horizontal) AND facingRight while the transitions from Run->Walk->Idle are just hSpeed. The transition between Idle R and L are just based on facingRight. The Left facing animations are the same as the right facing ones. I'm testing out a system where the same animation is used for right and left but the sprites can be different - so as not to have to animate again. Hence why I'm flipping the x scale but still using separate animations for each direction.
So I sort of solved the flipping - now it doesn't randomly have a pose in the opposite direction. I realized in my code I didn't account for what it does when getaxisraw horizontal is 0 so I set it up to face the direction of the previous frame: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$arionetteRootController : $$anonymous$$onoBehaviour {
Animator anim;
Rigidbody2D rbody;
bool prevFrameRight;
// Use this for initialization
void Awake ()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody2D>();
prevFrameRight = true;
}
// Update is called once per frame
void Update()
{
$$anonymous$$ove();
Turn();
}
void LateUpdate()
{
GetRootSpeed();
}
private void $$anonymous$$ove()
{
anim.SetFloat("hSpeed", Input.GetAxisRaw("Horizontal"));
}
private void Turn()
{
if (Input.GetAxisRaw("Horizontal") < 0)
{
gameObject.transform.localScale = new Vector3(-1, 1, 1);
anim.SetBool("facingRight", false);
prevFrameRight = false;
}
else if (Input.GetAxisRaw("Horizontal") > 0)
{
gameObject.transform.localScale = new Vector3(1, 1, 1);
anim.SetBool("facingRight", true);
prevFrameRight = true;
}
else if (Input.GetAxisRaw("Horizontal") == 0)
{
if (prevFrameRight)
{
gameObject.transform.localScale = new Vector3(1, 1, 1);
}
else
{
gameObject.transform.localScale = new Vector3(-1, 1, 1);
}
}
}
private void GetRootSpeed()
{
anim.SetFloat("rootSpeed", rbody.velocity.x);
}
}
Regardless, I still get the weird stuttering effect sometimes when it moves left. At times the amount is very small or nonexistent and at others its pretty bad. I kept the stats on for the gif files in case that lends anyone a clue. It can't be the animation itself because going right is fine and it uses the same clip to do so. Any suggestions?