Detect object at certain coordinates

Hello,

I’m trying to move an object across the screen and exectute a function, once it crossed a certain spot.

The object is translating along the x-axis.

That’s what I got so far:

function Update () {
	var translation = Time.deltaTime * -10;
	var myObject = GameObject.Find("MovingObject");
	myObject.transform.Translate (translation, 0, 0);
	
        if (myObject.transform.position == Vector3(147,9,-215)) {
		Debug.Log("object crossed a certain spot");
	}	
}

As I expected the “==” in my if statement just isn’t the right way to do it. Means, it doesn’t work :frowning:
The object is moving and even though it comes across the specified coordinates, the console does not show my Debug.Log message.

If anyone has an idea, how to detect this, I’d be more than happy.

Thanks in advance.

The simplest way would be to use a range:

if (Vector3.Distance(myObject.transform.position, Vector3(147,9,-215)) < 1)

(replacing 1 with any value that works, depending largely on how fast your object is moving)

Thanks! That did the trick :slight_smile: