my character does not turn left or right

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class player : MonoBehaviour
{

Rigidbody2D playerRB;
public float moveSpeed = 1f;

bool facingRight = true;

void Awake()
{

}

// Start is called before the first frame update
void Start()
{
    playerRB = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update()
{
    playerRB.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, playerRB.velocity.y);

    if (playerRB.velocity.x < 0 && facingRight)
    {
        FlipFace();
    }
    else if (playerRB.velocity.x > 0 && !facingRight)
    {
        FlipFace();
    }

}

void FixdedUptade()
{

}

void HorizontalMove()
{

}

void FlipFace()
{
    facingRight = !facingRight;
    Vector3 tempLocalScale = transform.localScale;
    tempLocalScale.x *= -1;
    transform.localScale = tempLocalScale;

}

}
In these codes, the character needs to turn left and right, but it doesn’t. However, I don’t see an error. Can you help me?

It is not recommended to use the local scale to flip the character, especially for visual purposes. It seems to me that the boolean is not public and you never change its value on any conditions, so it will always be true. Make it so that the boolean changes value based on user input (or however you want to detect a change in direction). That should fix your problem. However, I have a better method. I am assuming this game is in 2D which means this object would have a sprite renderer attached to it. The sprite renderer has a build-in method where if flips all the sprites on the X or Y-axis.

Here Is My Solution:


SpriteRenderer sr;//this will hold the sprite render component of this object

void Start(){
         sr = GetComponent<SpriteRender>();//gets the component
}

void FlipFace(){
          if(!facingRight)//if looking left flip sprite
                   sr.flipX = true;
          else//if lookng right reverse/don't flip
                   sr.flipX = false;
}

@tantuniman