Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by MountDoomTeam · Oct 12, 2012 at 08:13 PM · positionfindgame object

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));

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 29, 2012 at 07:41 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MountDoomTeam · Oct 13, 2012 at 02:07 AM 0
Share

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!

avatar image aldonaletto · Oct 13, 2012 at 03:28 PM 0
Share

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;
   }
 }
avatar image
1

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MountDoomTeam · Oct 13, 2012 at 02:12 AM 0
Share

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!

avatar image aldonaletto · Oct 14, 2012 at 04:22 PM 0
Share

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

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

if game object is located at... 1 Answer

Finding one of the several positions of a moving GameObject 1 Answer

Searching a List of GameObjects by name 2 Answers

How do I find the position of an element in an array? 3 Answers

Find position of a character of typogenic text 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges