• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Why are floats not accessing my inputted values? 1 Answer

C# move y position of object not working 2 Answers

GUIButton only executes once 2 Answers

How to rotate an object to face the direction it's going? 1 Answer

during build object moves different position than editor(vr) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges