Search Unity

Question Sprite doesnt flip after adding an animation

Discussion in '2D' started by damniam0, Apr 7, 2021.

  1. damniam0

    damniam0

    Joined:
    Apr 5, 2021
    Posts:
    6
    Hi guys
    I made animation for my character to move, but now it doesnt flip, when moving backwards (moveInput < 0f )
    What should I do?
    I will post entrie script.


    public class playerbiega : MonoBehaviour
    {

    private Rigidbody2D rb;
    private float moveInput;
    public float speed;

    public float dashSpeed;
    private float dashTime; //test
    public float startDastTime;
    private int direction;

    public Animator Animator;

    void Start()
    {
    rb = GetComponent<Rigidbody2D>();

    dashTime = startDastTime; //test

    Animator = gameObject.GetComponent<Animator>();
    }


    void Update()
    {
    moveInput = Input.GetAxis("Horizontal");
    if (moveInput > 0f)
    {
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    transform.localScale = new Vector2(0.09853891f, 0.09853891f);
    }
    else if (moveInput < 0f)
    {
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    transform.localScale = new Vector2(-0.09853891f, 0.09853891f);
    }


    if(moveInput != 0)
    {
    playerZmienne.isWalking = true;
    }
    else
    {
    playerZmienne.isWalking = false;
    }
    if (direction == 0) //dash
    {
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
    if (moveInput < 0)
    {
    direction = 1;

    }
    else if (moveInput > 0)
    {
    direction = 2;
    }
    }
    }
    else
    {
    if (dashTime <= 0)
    {
    direction = 0;
    dashTime = startDastTime;
    rb.velocity = Vector2.zero;
    }
    else
    {
    dashTime -= Time.deltaTime;
    if (direction == 1)
    {
    rb.velocity = Vector2.left * dashSpeed;
    }
    else if (direction == 2)
    {
    rb.velocity = Vector2.right * dashSpeed;
    } //koniec dash
    }
    }


    if (moveInput != 0)
    {
    Animator.SetBool("walking", true);
    }
    else
    {
    Animator.SetBool("walking", false);
    }
    }
    }
     
  2. damniam0

    damniam0

    Joined:
    Apr 5, 2021
    Posts:
    6
    Problem solver, I added

    private SpriteRenderer sprRend;

    in start
    sprRend = gameObject.GetComponent<SpriteRenderer>();

    void
    if (moveInput > 0f)
    {
    sprRend.flipX = false;
    }
    else if(moveInput < 0f)
    {
    sprRend.flipX = true;
    }

    In case someone will be in need