Search Unity

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:
    7
    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,164
  3. PerfectHiT

    PerfectHiT

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

    PerfectHiT

    Joined:
    Feb 16, 2018
    Posts:
    7
    It took me a few months but i fixed it and ill post the full Code incase someone need it someday

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.     [Header("MOVEMENT")]
    10.     public float playerSpeed;
    11.     private bool isMoving;
    12.     private Vector2 lastClickedPos;
    13.     private Vector2 currentPos;
    14.     private bool facingRight = true;
    15.  
    16.     [Header("COMPONENTS")]
    17.     private Animator anim;
    18.     private Rigidbody2D rb;
    19.     public SpriteRenderer sr;
    20.  
    21.  
    22.     void Awake()
    23.     {
    24.         anim = GetComponent<Animator>();
    25.         rb = GetComponent<Rigidbody2D>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         ClickToMove();
    31.         PlayAnimations();
    32.  
    33.  
    34.  
    35.  
    36.         if (facingRight == false && transform.position.x < lastClickedPos.x) Flip();
    37.         else if (facingRight == true && transform.position.x > lastClickedPos.x) Flip();
    38.  
    39.  
    40.  
    41.     }
    42.  
    43.     private void Flip()
    44.     {
    45.         facingRight = !facingRight;
    46.         Vector3 Scaler = transform.localScale;
    47.         Scaler.x *= -1;
    48.         transform.localScale = Scaler;
    49.  
    50.  
    51.     }
    52.  
    53.     private void PlayAnimations()
    54.     {
    55.         if (isMoving) anim.Play("Walk");
    56.         if (!isMoving) anim.Play("Idle");
    57.     }
    58.  
    59.     private void ClickToMove()
    60.     {
    61.         currentPos = transform.position;
    62.  
    63.         if (Input.GetMouseButton(0))
    64.         {
    65.             lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    66.             isMoving = true;
    67.         }
    68.  
    69.         if (isMoving && currentPos != lastClickedPos)
    70.         {
    71.          
    72.             transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, playerSpeed * Time.deltaTime);
    73.         }
    74.         else
    75.         {
    76.             isMoving = false;
    77.         }
    78.     }
    79. }
    80.