IF confition does not work even condition has met, why?

Hey everyone,

I got stuck on my project, I have 2 positions and when distance is zero it should go into the “if” condition and set “isCharging” false, but it keeps going without entering the if, you can see it on the pdf, values are same of both positions. I uploaded step by step but it keeps passing inside of “if”, please help.

(I also attached the same pdf.)

5145110–509411–Distance-Debug.pdf (371 KB)

Why on earth a pdf? Of all things. Just paste the code in Code-blocks .

The distance will (probably) never be exactly 0, just very near it. Check if the value is lower than some threshold instaed.

I wanted show debug process from VS :roll_eyes: , I tried that too changing " == 0 " to " <= 1f " also skips the “if” even values are same.

float and double are floating-point numbers, see Floating-point numeric types (C# reference).

Without going into detail about why your comparison fails, the right way to compare two floating-point values on equality in Unity is to use Mathf.Approximately (Scripting API).

“similar” means “as good as equal” here.

Here is my whole script; When enemy is close enough to the player, it charges toward to with extra speed. If “isCharging” false then it simply follow when it is close enough then enemy charges. But “isCharging” stuck on true because of that if condition. So using Vector3.Distance is wrong on my situation?

public class EnemyFollow : MonoBehaviour
{
    public float speed;
    public float chargingSpeed;

    private Transform target;

    private Animator animator;

    public bool isCharging;
    private Vector3 positionToChargeTowards;
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if (!isCharging)
        {
            if (Vector3.Distance(transform.position, target.position) > 6f)
            {
                transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

                animator.SetBool("ChargeUp", false);
            }
            else if (Vector3.Distance(transform.position, target.position) <= 6f)
            {
                positionToChargeTowards = target.position;
                isCharging = true;
            }

            if (transform.position.x < target.position.x)
            {
                transform.localRotation = Quaternion.Euler(0, 180, 0);
            }
            else
            {
                transform.localRotation = Quaternion.Euler(0, 0, 0);
            }
        }

        else
        {
            animator.SetBool("ChargeUp", true);
            Invoke("Charge",0.9f);

            if (Vector3.Distance(transform.position, positionToChargeTowards) == 0)
            {
                isCharging = false;
            }
        }
    }

    void Charge()
    {
        transform.position = Vector3.MoveTowards(transform.position, positionToChargeTowards, chargingSpeed * Time.deltaTime);
    }
}

Try replacing

if (Vector3.Distance(transform.position, positionToChargeTowards) == 0)
{
    isCharging = false;
}

with

if (Mathf.Approximately(Vector3.Distance(transform.position, positionToChargeTowards), 0))
{
    isCharging = false;
}

Don’t actually use Mathf.Approximately, as it’s threshold for “close enough to be the same” is too small to be useful. It’s generally a lot smaller than floating point errors. Your example in the latest post is functionally the same as == 0.

1 Like

Any advice how to fix my problem? I am trying to make “isCharging” true after the enemy charges.

As @Baste suggested, try checking against a treshold that is greater than 0, like 0.125f, 0.25f, …, whatever is appropriate in your setup.

I changed Update to FixedUpdate, and changed the distance

if (Vector3.Distance(transform.position, positionToChargeTowards) < 0.125f)
            {
                isCharging = false;
            }

This seems fixed the problem, thank you.