- Home /
How to get an object with exact coordinates?
Hello everyone, my first Question I'm posting. Basically what I'm trying to accomplish is getting an object(collider object) using the exact coordinates given. What I've been hoping and trying to accomplish is maybe I create a Ray right smack in the middle of the object I'm wanting to get, and see if I can get it that way but it doesn't seem to ever find it.
What I did earlier than that is create a ray using the given coordinates a bit down on the Y axis, and that works and all but is fairly glitchy and still not perfect.
So All I am wanting to do is just select an object at a specific location. If anyone has any ideas or needs further information let me know!
Thanks alot!
I don't get exactly what you want to do.. It sounds like you already have the objects coordinates and you want to shoot a ray at it using those coordinates to "get it".
I'm thinking, what you want to accomplish, is to shoot a raycast out towards an object to manipulate it accordingly by means of clicking the mouse button on the object, am I right?
Basically I was trying to use a raycast to start in the middle of the object and end right inside the same object to be able to select it but the raycast, being inside the object, never registered it(And keep in $$anonymous$$d this was just one of the ways I figured I could select that exact object was using a raycast inside of it). However, after further prolonged searching I came upon my solution. Here is my block of code:
Collider[] insideBlock = Physics.OverlapSphere(blockTestPos, 0.1f);
int i = 0;
for(i = 0; i < insideBlock.Length; i++)
{
if(insideBlock[i].gameObject.CompareTag("Block"))
{
AudioSource.PlayClipAtPoint(placeObject, insideBlock[i].transform.position);
dusty = (ParticleSystem)Instantiate(dust, insideBlock[i].transform.position, insideBlock[i].transform.rotation);
DestroyObject(insideBlock[i].transform.gameObject);
Basically what it does is searches using a sphere with an actual radius(something I was trying to accomplish by just putting a raycast inside of the object) and grabs the object for use at the moment(In this case I grab it to destroy it)
Answer by robertbu · Mar 08, 2014 at 06:48 AM
What you are asking is somewhat unclear, plus understanding why you want this calculation might lead to an alternate solution. With that said, you can use Physics.OverlapSphere() to get a list of any colliders that occupy the space at a specified position. The reference says it uses the collider bounds, but I believe from some testing that it is more accurate than that.
var colliders = Physics.OverlapSphere(somePosition, 0.0);
if (colliders.Length > 0) {
Debug.Log("You found "+colliders.Length + " at that position and the name of the first one is "+colliders[0].name);
}
else {
Debug.Log("No colliders found at that position");
}
If you need to see if an object is at the exact position, you will need to cycle through the colliders returned by that function and check to see if the transform.position matches the precise position you are looking for.
Note that the Raycast approach fails because meshes are one sided. That means a raycast from inside an object will not hit that object.
Thanks alot for the response and I do apologize for the unclearness of my question. Luckily I worked out the conclusion on my own, and I narrowed it down to exactly what you put. I do again apologize for the vagueness of my question, I had a real hard time myself trying to summarize the problem from my head to paper.
I'm not going to bother trying to explain what I was trying to do because it seems primitive and inefficient after finding out about Physics.OverlapSphere haha. Anyways, good day everyone and thanks for the help!