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 Flipping character's face not working with On click Movement

Discussion in 'Scripting' started by PerfectHiT, Nov 25, 2022.

  1. PerfectHiT

    PerfectHiT

    Joined:
    Feb 16, 2018
    Posts:
    2
    Hello everyone

    I'm trying to learn how to build a simple 2D RPG or a Moba and i'm struggling with Flipping my character Sprite when I move left/right/up/down. I've been learning 2d Side scrollers and apparently the flip function i'm trying to apply on the RPG game i'm building doesn't work per say.

    My Code:-

    Code (CSharp):
    1. public class PlayerMovements : MonoBehaviour
    2. {
    3.     [SerializeField] private float speed;
    4.     [SerializeField] private bool moving;
    5.     [SerializeField] private bool mouseDown;
    6.     [SerializeField] private bool facingRight = true;
    7.     Vector2 lastClickedPos;
    8.    
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         facingRight = true;
    14.     }
    15.  
    16.    
    17.     void Update()
    18.     {
    19.  
    20.         //Move on click
    21.         if (Input.GetMouseButtonDown(0))
    22.         {
    23.             lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    24.             moving = true;
    25.  
    26.         }
    27.  
    28.         if(moving && (Vector2)transform.position != lastClickedPos)
    29.         {
    30.             float step = speed * Time.deltaTime;
    31.             transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
    32.             Flip();
    33.                
    34.         }
    35.  
    36.         else
    37.         {
    38.             moving = false;
    39.         }
    40.  
    41.         // Hold mouse botton down to move like diablo
    42.  
    43.         if (Input.GetMouseButtonDown(0))
    44.         {
    45.             mouseDown = true;
    46.         }
    47.         if (Input.GetMouseButtonUp(0))
    48.         {
    49.             mouseDown = false;
    50.         }
    51.         if (mouseDown)
    52.         {
    53.             lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    54.             moving = true;
    55.         }
    56.  
    57.         //flipping character face
    58.         if (facingRight == false && this.transform.position.x > 0) Flip();
    59.         else if (facingRight == true && this.transform.position.x < 0) Flip();
    60.  
    61.  
    62.     }
    63.  
    64.     private void Flip()
    65.     {
    66.         facingRight = !facingRight;
    67.         Vector3 Scaler = transform.localScale;
    68.         Scaler.x *= -1;
    69.         transform.localScale = Scaler;
    70.  
    71.  
    72.     }
    73.  
    74. }
    What my code does is flipping the character Sprite only when my position is literally less or greater than 0 which is working perfectly when i use Input.GetAxis("Horizontal"); method and use my keyboard to move the character but when i use mouse movements it waits until it reach position 0 and then flip accordingly.

    To make it much more clearer, here is a video :


    Could you please help me with this? i'm stuck on it.
    Thanks alot..
    Onix
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,051
  3. PerfectHiT

    PerfectHiT

    Joined:
    Feb 16, 2018
    Posts:
    2
    the same problem happened using SpriteRenderer.flipX. My character does not flip its face unless it reached the 0 position.