Enemy character glitches when trying to flip via transform

Im making a 2D game where i’m making a flying enemy. I used A* Pathfinding Project for the movement AI of the enemy. But im stuck at flipping the enemy when going the opposite way. i have writting a simple code that was explained in the same video that explained how to use A* pathfinding, but it tranforms the size of the enemy, and when it is going to the left, it rapidly flips the sprite.

Here is the code that i wrote:

using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using Pathfinding;



public class FlyingEye : MonoBehaviour
{
    public DetectionZone biteDetectionZone;


    Animator animator;
    Rigidbody2D rb;



    public Transform target;
    public float speed = 200f;
    public float nextWaypointDistance = 3f;



    Path path;
    int currentWaypoint = 0;
    bool reachedEndOfPath = false;
    Seeker seeker;






    private void Awake()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        seeker = GetComponent<Seeker>();

        InvokeRepeating("UpdatePath", 0f, .5f);
        
    
    
    }

    void UpdatePath()
    {
        if (seeker.IsDone())
            seeker.StartPath(rb.position, target.position, OnPathComplete);
    }




    public bool _hasTarget = false;

    public bool HasTarget 
    {
        get
        {
            return _hasTarget;
        }
        private set
        {
            _hasTarget = value;
            animator.SetBool(AnimationStrings.HasTarget, value);
        }
    }

    public bool CanMove
    {
        get
        {
            return animator.GetBool(AnimationStrings.canMove);
        }
    }


    void FixedUpdate()
    {
        HasTarget = biteDetectionZone.detectedColliders.Count > 0;

        if (path == null)
            return;

        if(currentWaypoint >= path.vectorPath.Count)
        {
            reachedEndOfPath = true;
            return;
        } else
        {
            reachedEndOfPath = false;
        }
        
        Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
        Vector2 force = direction * speed * Time.deltaTime;

        rb.AddForce(force);

        float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);

        if (distance < nextWaypointDistance)
        {
            currentWaypoint++;
        }
        
        if (force.x >= 0.01f)
        {
            transform.localScale = new Vector2(transform.localScale.x * 1f, transform.localScale.y);
        }
        if (force.x <= -0.01f)
        {
            transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
        }
    }



    

    void OnPathComplete(Path p)
    {
        if(!p.error)
        {
            path = p;
            currentWaypoint = 0;
        }
    
    }
















}

does someone have an idea how to do this properly?

So, in this line:

transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);

You’re multiplying your local scale *-1, which means that it doesn’t matter if it is on a negative scale currently, it will flip it every frame.

You can solve this by checking if the local scale is already negative

if (force.x <= -0.01f && transform.localScale.x > 0.001f)
{
    transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
}

Or have a set variable that stores the localScale of your character at the beggining and when you’re going to set your localScale you just call it:

if (force.x >= 0.01f)
{
    transform.localScale = new Vector2(storedScale.x, storedScale.y);
}
if (force.x <= -0.01f)
{
    transform.localScale = new Vector2(-storedScale.x, storedScale.y);
}

I implemented your code and it works. BUT now if I go to the left of the enemy, it flips. and if I go to the right of the enemy again. It doesn’t flip anymore.

Here is the code:

if (force.x >= 0.01f && transform.localScale.x < -0.001f)
{
    //FlipDirection();
    transform.localScale = new Vector2(transform.localScale.x * 1f, transform.localScale.y);
}
if (force.x <= -0.01f && transform.localScale.x > 0.001f)
{
    //FlipDirection();
    transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
}

Ah, I see what’s happening.

if (force.x >= 0.01f)
{
    //FlipDirection();
    transform.localScale = new Vector2(transform.localScale.x * 1f, transform.localScale.y);
}
if (force.x <= -0.01f && transform.localScale.x > 0.001f)
{
    //FlipDirection();
    transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
}

The line I recommended you to use was only necessary in the negative one. So this should technically work. If it doesn’t then please reply so I can help you further.

This should work perfectly

if (force.x >= 0.01f && transform.localScale.x < -0.001f)
{
    transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
}
else if (force.x <= -0.01f && transform.localScale.x > 0.001f)
{
    transform.localScale = new Vector2(transform.localScale.x * -1f, transform.localScale.y);
}