Home > Back-end >  Weird position flip animation scale
Weird position flip animation scale

Time:12-16

I'm new to unity and I'm taking a course. In this case, I was trying to flip my animation, so when the character turns it flipped the animation. I was following the codes that are provide through the internet course, as usual when flipping animation it uses the transform.localscale and make the x as -1 so it flips. But the weird thing is, every time I tried to toggle the x scale to -1, it sort of blink the position

This happen when i tried it: facing right moving right but when i tried to move left, this happen: facing left moving left

It just blinks the position, the result I expected was just to mirror the animation without changing its position.

This is my code:

theRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, theRB.velocity.y);

if (theRB.velocity.x < 0)
{
    transform.localScale = new Vector3(-1f, 1f, 1f);
}
else if (theRB.velocity.x > 0)
{
    transform.localScale = Vector3.one;
}

CodePudding user response:

Make sure that in the hiearachy the standing sprite's x position is 0.

If it's not, then changing the scale of its parent will blink its position on screen.

And instead of changing scale, you can change the Sprite Renderer's FlipX setting to flip your sprite, like:

spriteRenderer.flipX = theRB.velocity.x < 0;

https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html

CodePudding user response:

Make sure that the pivot point of your character's model is centered. If the pivot point is not centered, flipping the animation using transform.localscale may cause the character to appear to move or shift.

Check your animation settings. If your character's animation is set to "Loop Time," this could cause the animation to reset every time you toggle the transform.localscale property. You can try setting the animation to "Ping Pong" or "Clamp Forever" to see if this resolves the issue.

Check for any errors in your code. It's possible that there could be an error in your code that is causing the animation to behave unexpectedly. Make sure to double-check your code and look for any syntax errors or logical issues.

Make sure that you are using the correct animation controller. If you have multiple animation controllers in your project, make sure that you are using the correct one for your character.

I hope these suggestions help!

  • Related