- Home /
Checking for object in position
Hi, i'm trying to make it so when an object moves to a specific position it triggers an event (Printing a message currently) I've been looking for a while and can't figure it out.
So far my code is
if (Physics.Raycast(transform.position,Vector3.Equals(0,0,0))) { Debug.Log("In Position"); }
This doesn't work though, i'm not really sure what i'm doing so would appreciate any help!
Thanks in advance!
Could you expand on what you mean by "when an object moves to a specific position"?
Typically when you want functionality like that you'd create a trigger collider (e.g., an invisible sphere) and then have your code run within OnTriggerEnter. If you wanted to know when that object is touched (as opposed to being inside), you could use OnCollisionEnter. (Both of these calls would require a rigidbody on either the collider or the object).
A raycast on the other hand fires an invisible ray from a position along a direction and is typically better suited for answering questions like "what is my character looking at" or "is there an object of type x in front of my character".
I'll do my best to explain it so apologies if I don't explain well.
I have an object that spawns in and starts moving. I want it to stop at a specific point then trigger the next event. Would I need to use OnTriggerEnter for this to happen? Thanks for helping!
I would think so. If you wanted to test if an object was at a very specific spot (like pushing a cube onto a pressure plate in a Zelda game), then I would probably create a trigger there. But if you wanted something more dynamic, like "the object walks 10 meters and then turns around" then I would do it by testing position directly.
Answer by tecses1 · Mar 05, 2020 at 12:28 AM
There are multiple ways to handle this. Firstly,
if (Physics.Raycast(transform.position,Vector3.Equals(0,0,0))) { Debug.Log("In Position"); }
This is casting a ray from your transform.position in the direction of (0,0,0), so in other words, nowhere. A raycast is used to send a ray out from an origin and see what it hits, and I'm not sure this is what you want. You could try
transform.position.Equals(desiredPosition);
But keep in mind if you're using anything but whole numbers, checking floats is extremely difficult because of the precision. So I would recommend two things:
1) Tag your player something like "Player", and then create an object with a collider trigger, and use OnTriggerEnter to find if the player hit it, like:
private void OnTriggerEnter(Collider other) {
if (other.transform.tag == "Player") {
Debug.Log("In position");
}
}
2) Or, check the distance between the desired position and your position so you can have a range, Like:
if (transform.position.Distance(desiredPosition) < 0.5f){
Debug.Log("Within 0.5 meters of position!");
}
Thanks for the reply! I keep getting an error on the transform.positon.Equals
which is saying 'no overload for method 'Distance/Equals' takes 3 arguments
void Update()
{
transform.position += transform.forward * -1 * Time.deltaTime;
if (transform.position.Equals(0,2,5))
{
Debug.Log("In Position");
}
}
is my full code for the script
Just a heads up, Vector3.Equals is not a great tool for equality unless you're specifically certain that is what you want to use. ==
is a better fit because it compensates for floating point inaccuracies (whereas transform.position.Equals
doesn't).
That said, in your code, Equals() is expecting a single object and you're technically giving it 3 objects. You'd want to give it an actual vector3 object and use that, like transform.position.Equals(destination)
.
protected vector3 destination = new Vector3(0f, 2f, 5f);
//if you wanted to specify the value in-code. 'f' is used after numbers to indicate that they are floats, ins$$anonymous$$d of converting from int to float. A vector3 is a float data-type.
public vector3 destination; //if you wanted to specify the value in the inspector
void Update()
{
transform.position += transform.forward * -1f * Time.deltaTime;
if (transform.position == destination)
{
Debug.Log("In position");
}
}
That's because Transform.position.Equals needs a vector3 object for comparison. You are passing 3 integers. (0,2,5) is very different from (new Vector3(0,2,5))! I would of course recommend using the distance solution ins$$anonymous$$d!
Your answer
Follow this Question
Related Questions
Why Does My Object Change Position During Update? 0 Answers
Remove object based on its position alone 1 Answer
Collide edge of object with the edge of the screen 0 Answers
Camera rotation around player while following. 6 Answers
How to segment a number line of game objects between two points dynamically? 1 Answer