- Home /
2d character not flipping ,2D animation not Flipping. HELP
My character wont flip while having the animator turned on but when its off the character flips Here's my code using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class movement : MonoBehaviour { public float speed; public float jumpForce; private float moveInput; private Rigidbody2D rb; private bool facingRight = true; private bool isGrounded; public Transform groundCheck; public float checkRadius; public LayerMask whatIsGround; private int extraJumps; public int extraJumpValue; public Animator animator; public GameObject GameOverText, RestartButton;
void Start()
{
extraJumps = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
GameOverText.SetActive(false);
RestartButton.SetActive(false);
animator = GetComponent<Animator>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround );
moveInput = CrossPlatformInputManager.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
animator.SetBool("IsRunning", false);
}
else
{
animator.SetBool("IsRunning", true);
}
if (facingRight==false&& moveInput > 0)
{
flip();
}
if(facingRight==true && moveInput < 0)
{
flip();
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpValue;
animator.SetBool("IsJumping", false);
}
else
{
animator.SetBool("IsJumping", true);
}
if (CrossPlatformInputManager.GetButtonDown("Jump") && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
animator.SetTrigger("takeoff");
}
else if(CrossPlatformInputManager.GetButtonDown("Jump")&& extraJumps==0 && isGrounded==true)
{
rb.velocity = Vector2.up * jumpForce;
animator.SetTrigger("takeoff");
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Enemy"))
{
GameOverText.SetActive(true);
RestartButton.SetActive(true);
gameObject.SetActive(false);
}
}
void flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
} ,Im following Blackthornprods animation transition and 2d platformer tutorials (links: https://www.youtube.com/watch?v=FTxQKHG5WCA&t=812s https://www.youtube.com/watch?v=QGDeafTx5ug) and in it he uses this function to flip the character void flip() { facingRight = !facingRight; Vector3 Scaler = transform.localScale; Scaler.x *= -1; transform.localScale = Scaler;
}
I followed his steps one to one but my character wont flip
Answer by afraism · Feb 04, 2021 at 06:45 PM
Make sure that the gameObject to be flip don't have any animation key on scale in the animation. If this is the case, delete the keys related to the scale. If you do not understand what I mean, send a photo of the animation timeline.
Answer by EvanCheddar · Feb 04, 2021 at 10:47 PM
Try flipping sprites with eulerAngles instead, something like this:
transform.eulerAngles = new Vector3(0, 0, 0); //facing right
facingLeft = false;
transform.eulerAngles = new Vector3(0, 180, 0); //facing left
facingLeft = true;
Okay now when the characters sprite is flipping left but it doesnt flip to the right when turning right.
Your answer
Follow this Question
Related Questions
2D Sprite Fillup without using UI 1 Answer
How can I stop a glitch where my character transitions from left to right he snaps back? 0 Answers
Issue with Animating MultiPart 2D Character with RigidBodies 0 Answers
,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers
How to sync animation if animation should be use with specially object 0 Answers