- Home /
Find a GameObject exactly in position Vector3(1,2,3)?
i wish to find a game object that I know will be exactly on a precise coordinates-it would seem logical as it could be faster than scanning/Collider algorithms for some tasks. So a variable of the kind:
var Object_1 = gameobject.findinposition(Vector3(1,2,3));
really, could you simply search through them - carefully using $$anonymous$$athf.Approximately?
Remember, that Vector3 is composed of floats -- not integers.
Aways, on computers, comparing floats is problematic.
So to begin with likely what you want is something annoying like
2.99 < x < 3.01, right?
(only you would know the tolerance based on your situation)
Very generally to find an object "in a specific place" you would use a quadtree. But just as Aldo explains, the whole of PhysX is at your disposal, to solve exactly this problem. The whole nature of a game engine is it solves exactly the issue you raise! So, you can use it.
Answer by aldonaletto · Oct 12, 2012 at 08:47 PM
You could use Physics.OverlapSphere with a very small radius - but be aware that this function returns an array with all colliders whose bounding boxes intersect the logical sphere, thus other near objects may be included, even if they visually don't intersect the test point. Assuming that the object of interest should be at that position, we could return the nearest object, like below:
function FindAt(pos: Vector3): GameObject {
// get all colliders that intersect pos:
var cols = Physics.OverlapSphere(pos, 0.1);
// find the nearest one:
var dist: float = Mathf.Infinity;
var nearest: GameObject;
for (var col: Collider in cols){
// find the distance to pos:
var d = Vector3.Distance(pos, col.transform.position);
if (d < dist){ // if closer...
dist = d; // save its distance...
nearest = col.gameObject; // and its gameObject
}
}
return nearest;
}
Thankyou, it's for a procedural editor that edits some tiles, i am changing the tiles next to the spaces in the selection, so i know there will always be a tile there, it's actually a program with thousands of tiles, so i am using overlapsphere at the moment, which is great, although i wanted to make everything fast and figured maybe there is a way to take the tile in pos Vector3(1,2,3) in a more efficient way than by the overlap sphere version. Thanks!
Well, if the objects are disposed on a plane, you can use a up-down raycast from a position a little above the plane - this may be faster, since only one object will be returned. Supposing that the tiles are on the XZ plane, the code could be as simple as this one:
function FindAt(pos: Vector3): GameObject {
pos.y += 1; // shift the position 1 unit above
var hit: RaycastHit;
// cast a ray downwards with range = 2
if (Physics.Raycast(pos, Vector3.down, hit, 2)){
return hit.collider.gameObject;
} else {
return null;
}
}
Answer by echofiend · Oct 12, 2012 at 10:39 PM
If you know a gameobject will be in a specific position without doubt, do you also know what gameobject will be in that position? can you just grab the object when you need it?
var gameObject = GameObject.Find("known object");
I am guessing that you dont know WHAT object will be there, just that an object WILL be there, so another idea could be, set a game object w/collider in that position to be a trigger, and when the object enters the collider grab the name/object/run function what ever you want to do then.
function OnTriggerEnter (other : Collider) {
var name = other.name;
other.SendMessage("FoundObject",name);
}
conclusion. no solution to find object on transform.position, except if it has collider by raycast and sphereoverlap, and by name, tag, etc.
hi there, yes i do know the gameobject, but there are 2000 similar of them, all called "tile"; ! so i figured there could be a logical option in unity to say "delete any gameobject at vector3 (123).
perhaps there is also raycast, although that scans every pixel on a line, wheras on trigger and overlapshere is all within a radius, and ontrigger is all events on a radius. hmm.
it would have been logical to have a command that finds an object on a know coordinate also!
thanks!
Actually, raycast doesn't scan every pixel on a line: it's a logical operation performed by the physics engine like OverlapSphere, but probably faster since only a single object is returned. The physics engine uses highly optimized spatial search algorithms and data structures, thus it can quickly find the nearest collider hit by a raycast
Answer by CandelaPrime · Aug 24, 2018 at 01:50 PM
If your tiles are all arranged on a plane, wouldn't it be easiest to just store them in a 2D array and get them by index?