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 /
This question was closed Oct 08, 2013 at 01:14 AM by DayyanSisson for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DayyanSisson · Oct 08, 2013 at 12:59 AM · transformdistancetransformpointtransform.transformpoint

Comparing World Positions

So I've been trying to find the closest node to a certain object:

     void FindClosestNode (Transform comparitive) {
 
         float closeDistance = Mathf.Infinity;
         GameObject closestNode = null;
         GameObject[] gNodes = GameObject.FindGameObjectsWithTag("Waypoint");
 
         foreach(GameObject node in gNodes){
             float dis = (node.transform.position - comparitive.position).sqrMagnitude;
             if(dis < closeDistance){
                 closestNode = node;
                 closeDistance = dis;
             }
         }
 
         end = closestNode.transform;
     }

So all looks fine and dandy; I've used this code for countless other scripts trying to find the closest.... something or other. But, for some reason this code does not work. No matter where I put the comparative, it always chooses this same node. Now I checked the Transforms on each object and I noticed that the comparative was in world space (it's position was roughly (-12,21,-52)) and for some reason the node (in which the comparative was quite literally standing on) was (-0.0001506805, 0.0002708435, 14.63168) which made absolutely no sense to me since the node was more or less inside the comparative. I realized that the node was a child of another object at (0,0,0). So I figured it must be working in local space. So I tried changing the distance code from:

 float dis = (node.transform.position - comparitive.position).sqrMagnitude;

To:

 float dis = (node.transform.TransformPoint(node.transform.position) - comparitive.position).sqrMagnitude;

Using transform.TransformPoint to convert it to world space. But the code still doesn't work. Anything that looks wrong?

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Oct 08, 2013 at 01:03 AM

Transform.position is always world space. Transform.localPosition is relative to the parent. If you use transform.position you don't need to use TransformPoint or anything else.

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 DayyanSisson · Oct 08, 2013 at 01:14 AM 0
Share

Wow, this has been happening a lot lately. Just realized that the objects weren't tagged "Waypoint".....

.....sigh.

avatar image Eric5h5 · Oct 08, 2013 at 01:19 AM 0
Share

It's always something like that. ;)

Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to calculate distance between objects? 5 Answers

charachters stand next to each other 0 Answers

Camera distance from mesh not center 1 Answer

Distance won't work 1 Answer

Check distance between multiple objects 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