Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by d3vilkat · Apr 09, 2017 at 05:32 AM · c#positionobjectscheck

Check if two objects have the same position.

I have several game objects in my scene that are obstacles, and because they are procedurally generated I want to check in case two of them have the same position.

PD: sorry for my english.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by alph1 · Apr 21, 2017 at 04:18 PM

You can assing a tag and with using this tag you can call all objects that holds the same tag into a gameObjects list: List<GameObject> _list = GameObject.FindAllGameObjectsWithTag(YOURTAG) and after that you can check if there are objects that holds same position like:

  for(int i = 0;i<_list.length;i++)
     {
         for(int p = 0;p<_list.length;p++)
         {
            if(i != p)
            {
                if(_list[i].transform.position == _list[p].transform.position)
                 {
                   Debug.Log("Same Position!");
                  }
            }
        }
     }

If you want to spawn an object(OBSTACLE) that holds different position from all others before it you can write a bool function which returns true-false if there is a objects that have same position so you can spin random once again to find different position as:

 bool checkSame(List<GameObject> _list)
 {
 for(int i = 0;i<_list.length;i++)
      {
          for(int p = 0;p<_list.length;p++)
          {
             if(i != p)
             {
                 if(_list[i].transform.position == _list[p].transform.position)
                  {
                   return true;
                   }
             }
         }
      }
 return false;
 }

This function will return if there are two objects that have same position.

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 agencebrunel · Jul 19, 2017 at 04:52 PM 0
Share

Hello, i want to use your solution but i dont understand your code.

Why ; if(i != p) ? If you list all gameobject with same tag in _list. then you put i and p the same value, that inside _list. So i and p will never be different in your code ?

avatar image agencebrunel · Jul 19, 2017 at 05:02 PM 0
Share

Ho i understand !

if(i != p) mean if they are not the same object !

But i and p are int ?

That not (_list[i] != _list[p]) you want to writte ?

Because of course same object will have the same position...

avatar image
0

Answer by diegzumillo · Apr 09, 2017 at 05:26 PM

One way of doing this is to have whatever script is managing these instantiations to keep a list of references of these objects. I don't know if these objects are being generated all at once at startup or continually through the game but, in any case, before creating one object you check the list of current objects and see if the intended position for the new object is already occupied by someone on the list. If not, instantiate the object and add it to the list.

To check for intersection you could use transform.position simply (if the magnitude of pos1 - pos2 is smaller than some threshold return true) but that doesn't guarantee there won't be intersection if you have objects with largely different shapes and sizes. The ideal solution depends on what you're doing, really. Maybe intersection is not a problem. If it is, I'd probably use physics collision system, with some function like physics.spherecast or cube or capsule etc. Or maybe you are instantiating things on a grid, then instead of physics, or transform.position I'd use simply the grid coordinates.

I hope any of this helps.

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

7 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Checking position 2 Answers

Get the amout of objects in a given position 1 Answer

Multiple Cars not working 1 Answer

Objects not always Spawning At Correct Location 2 Answers


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