Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question 2d Character doesn't flip after adding a animator

Discussion in '2D' started by Lennnart, May 26, 2021.

  1. Lennnart

    Lennnart

    Joined:
    May 24, 2021
    Posts:
    3
    Hello, (sorry, if my English is bad I'm German)
    I started learning unity and programming a few days ago. I followed the Brackeys 2d game tutorials but now I'm stuck. When I add a animator the player is facing right when I move right and also when I move left. Before I added the animator everything was working fine.
    The only difference I made to Brackeys game is that I used the built in animation function and Brackeys is using a sprite animation and I used the same code.

    script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playermovement : MonoBehaviour {
    6.  
    7.     public CharacterController2D controller;
    8.     public Animator animator;
    9.    
    10.     public float runSpeed = 40f;
    11.  
    12.     float horizontalMove = 0f;
    13.     bool jump = false;
    14.     bool crouch = false;
    15.  
    16.  
    17.    
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    23.  
    24.         animator.SetFloat("speed", Mathf.Abs(horizontalMove));
    25.  
    26.  
    27.         if (Input.GetButtonDown("Jump"))
    28.         {
    29.             jump = true;
    30.         }
    31.  
    32.         if (Input.GetButtonDown("crouch"))
    33.         {
    34.             crouch = true;
    35.         } else if (Input.GetButtonUp("crouch"))
    36.         {
    37.             crouch = false;
    38.         }
    39.     }
    40.  
    41.  
    42.     void FixedUpdate ()
    43.  
    44.     {
    45.         // Move our Character
    46.         controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    47.         jump = false;
    48.     }
    49. }
     
  2. MichaelH55

    MichaelH55

    Joined:
    Jan 19, 2014
    Posts:
    19
    Moin Lennart,

    you have to flip your character. You can do that in different ways, here is one:

    Create a bool

    Code (CSharp):
    1. private bool isFacingRight = true;
    2.  
    In Update, after you get youre horizontal move

    Code (CSharp):
    1. if ((horizontalMove < 0f && isFacingRight) || (horizontalMove > 0f && !isFacingRight))
    2. {
    3. isFacingRight = !isFacingRight;
    4. transform.Rotate(0.0f, 180.0f, 0.0f);
    5. }
     
    Ted_Wikman and Lennnart like this.
  3. Lennnart

    Lennnart

    Joined:
    May 24, 2021
    Posts:
    3
    Thank you it finally works!