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
0
Question by ajasona · Aug 23, 2017 at 03:02 PM · c#transformpositionfindgameobjectswithtaglocalposition

Find a gameobject with the same position

I want to check if another gameobject has the same position!

 public void update()
     {
         if (GameObject.FindGameObjectWithTag("wall").transform.localPosition == gameObject.transform.localPosition)
         {
            //something happens
         }
       
     }





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
1
Best Answer

Answer by M-G-Production · Aug 23, 2017 at 04:12 PM

First you should consider storing that 'wall' into a local variable to increase performance.

Then you should use position instead of localPosition (position is the real world position, localPosition is the position as child relative to its parent.)

Finally, I'd say that the chance of the wall being exactly at the same place could be pretty hard to validate (depending on the way you are programming your game)... You could create a method that could compare the values with a certain distance...

Adjust your script like this:

 GameObject theWall;
     public void update()
     {
         if (theWall == null)
             theWall = GameObject.FindGameObjectWithTag("wall");
 
         if (AreNear(theWall.transform.position, gameObject.transform.position, 0.1f))
         {
             //Something Happens
         }
     }
 
     //This method detects if the object are close depending on minDistance value
     bool AreNear(Vector3 obj1, Vector3 obj2, float minDistance = 0)
     {
         return ((obj1.x < obj2.x + minDistance && obj1.x > obj2.x - minDistance) &&
             (obj1.y < obj2.y + minDistance && obj1.y > obj2.y - minDistance) &&
             (obj1.z < obj2.z + minDistance && obj1.z > obj2.z - minDistance));
     }
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 Bunny83 · Aug 23, 2017 at 04:38 PM 0
Share

Update need to start with an uppercase letter. $$anonymous$$aybe the code in question was just pseudo code, but when writing answers they should be correct.

avatar image ajasona Bunny83 · Aug 24, 2017 at 11:47 AM 0
Share

Thx! I did forgot the uppercase letter!

avatar image
1

Answer by spraw · Aug 23, 2017 at 05:21 PM

 bool IsInDistance(Transform a, Transform b, float maxDistance)
     {
         return Vector3.Distance(a.position, b.position) <= maxDistance;
     }

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

90 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 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 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 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 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 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 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

Locking gameobject to rotating floor 0 Answers

C# round transform.position floats to .5 2 Answers

stop object(door) translation 0 Answers

Not finding closest Object with tag player. 2 Answers

Obtaining the relative size of a Models body part 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