My IF Statement is not executing

Hello :slight_smile:
First time posting so forgive me for any community mistakes.
Below is my script attached to the “Enemy Sprite” GameObject.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyLogic2 : MonoBehaviour
{
    public Rigidbody2D enemyRb;
    public Transform point0;
    public Transform point1;
    public Transform point2;
    public Transform point3;
    public Transform point4;

    // Start is called before the first frame update
    void Start()
    {
     
    }

    // Update is called once per frame
    void Update()
    {
     
    }

    private void FixedUpdate()
    {
        if (transform.position == point0.position)
        {
            //transform.position = Vector3.MoveTowards(transform.position, point1.position, moveSpeed * Time.deltaTime);
            enemyRb.velocity = Vector2.up * 30 * Time.deltaTime;
        }

        else if (transform.position == point1.position)
        {
            enemyRb.velocity = Vector2.down * 30 * Time.deltaTime;
            Debug.Log("Executed");
        }

        Debug.Log("Player Position:" + transform.position);
        Debug.Log("Point1 Position" + point1.position);
    }
}

Below is an image of my game scene, hierarchy and console w/ Debug.

9860265--1420446--Screenshot 2024-05-28 191331.png

When I run the scene, the first IF statement executes.
However, the second IF statement does not execute. Yet, in the console, you can see that the position of EnemySprite Transform EQUALS the position of POINT1.

(VECTOR3)(-10.56, 8.23, 0,00) == (VECTOR3)(-10.56, 8.23, 0,00)

What could be the reason? Could there be more digits after 8.23XXXX that is not equal to the other vector?
How can I check something like that?

ALSO, when I move the EnemySprite to POINT1 then run the scene, the second IF statement executes but not the first.

Thanks in advance!

Comparing equality on floating point numbers is more often than not going to fail due to their imprecision. You generally want to check approximate equality (such as with Mathf.Approximately) or simply check if a value is greater or less than.

Print out the individual x, y and z values so you can see all their decimal points. You’ll find they will be off by tiny margins. Printing out Vector3 values as-is just truncates it to two decimal points for readability.

2 Likes

Looking at your setup, and I’m only judging from the static screen, you might have better luck checking the Vector3.Distance(a,b) between two points and asking if it is below a certain amount, something like:

if (Vector3.Distance( transform.position, point0.position) < 1.0f)
{
  // we are within 1.0 distance of point0!
}
/// etc... for all the things you're checking...
3 Likes

Thank you Spiney!

9860331--1420449--Screenshot 2024-05-28 201638.png

You are correct. It seems that the X position jumps from 8.228825 to 8.238506. It skips X8.23. I will learn about Mathf.Approximately and attempt to fix it.

Thanks again!