Compare two coordinates

Hi.

How do I compare two coordinates?

Example:

void Update()
    {
        Vector2 character = GameObject.Find("Idle").transform.position;
        Vector2 objects = Vector2 (0.0, 0.0);

        if(character == objects)
        {
            Debug.Log("Teste");
        }
    }

Thank You

That is how you do it.

Note that transform.position is a Vector3 so when you implicitly convert it to Vector2 it drops the Z component

Sorry, I did not understand how it works, Please could you give me an example.

public static bool operator ==(Vector2Int lhs, Vector2Int rhs);

Thank You

Your code is correct.

if (v1 == v2)
{
    Debug.Log("same");
}
1 Like

I assume it doesn’t work for you, because the Vector2’s would have to be really really close together.
You can roll your own implementation to better fit your requirements.
For example:

public static class MyMath
{
    public static bool Equal(Vector2 _v1, Vector2 _v2, Vector2 _e)
    {
        return System.Math.Abs(_v1.x - _v2.x) <= _e.x &&
               System.Math.Abs(_v1.y - _v2.y) <= _e.y;
    }
}

Then use it like this:

void Update()
{
    Vector2 character = GameObject.Find("Idle").transform.position;
    Vector2 objects = new Vector2(0.0f, 0.0f);
    Vector2 maxOffset = new Vector2(0.5f, 0.5f);

    if(MyMath.Equal(character, objects, maxOffset))
    {
        Debug.Log("Character and Objects are closer than maxOffset.");
    }
}
1 Like

The equality operator already does this. From the page I linked:

1e-5 is rather small. Depending on the usecase this value is too small to ever trigger.

I mean…you’re checking equality…

If you want to check close distance then I would compare the magnitude of the difference to some given threshold. Also worth noting that Unity’s Mathf has an Approximately method that checks against epsilon already. So you could do this

return Mathf.Approximately(v1.x, v2.x) && Mathf.Approximately(v1.y, v2.y);
2 Likes

I agree that calling the method ‘Equal’ is a bit misleading.

Mathf.Approximately still has the same problem. It checks against a very small value, which i believe is also 1e-5.

It really depends on what the OP is using it for.

Approximately uses epsilon

wow, It’s very very confused to me. This is correct ? Please, explain me step by step ok.

First, my current code: What is it wrong?

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

public class coin : MonoBehaviour {

    void Update()
    {
        Vector2 character = GameObject.Find("Idle").transform.position;
        Vector2 objects = new Vector2(0.0f, 0.0f);
        Vector2 maxOffset = new Vector2(0.5f, 0.5f);

        if(MyMath.Equal(character, objects, maxOffset)
        {
                Debug.Log("Character and Objects are closer than maxOffset.");
        }
    }

}

Thank You

You are probably missing the other code i posted, namely the MyMath class.
Just put that into another script file somewhere in your project.
You can then access it from anywhere, since it is a static class with a static method.

ok, but How could I get the character position and compare with my ( maxOffset )? This is my doubt.

bool isEqual(float a, float b) 
{
    if (a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
       return true;
    else
       return false;
}

Thank You

Approximately? scoffs like an elitist That function can generate garbage. I find that to be a big deal for a simple math operation, thus I simply check against float.Epsilon.

substract the two positions together then see if result.sqrMagnitude is less than maxOffset*maxOffset. if so the 2 positions are within the maxoffset.

2 Likes

Indeed. Know your target audience etc etc :slight_smile:

Again this really boils down to are you checking equality or are you checking a distance threshold