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
3
Question by Tyler 2 · Dec 23, 2010 at 08:19 PM · objectdistance

How can I get the distance between two objects?

Hello. How can I get the distance between two (moving) objects and then have a function dependent on the distance (ex: if distance > 10...)? Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by The_r0nin · Dec 23, 2010 at 08:38 PM

var obj1 : Transform; var obj2 : Transform;

function Update() { var distance = Vector3.Distance(obj1.position, obj2.position); if (distance > 10.0f) { // do your code here } }

Though, in terms of performance, you might be better off using the square of your distance (fewer square roots to calculate) and do something like:

var sqrDistance = (obj1.position - obj2.position).sqrMagnitude;

// Note you also need to calculate the square of the test, // so 100.0f = 10.0f * 10.0f if (sqrDistance > 100.0f) { // do your code here }

Comment
Add comment · Show 3 · 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 Statement · Dec 23, 2010 at 11:39 PM 0
Share

I took the liberty to tidy up your answer and clarify the 100.0f distance. Well thought with the performance.

avatar image The_r0nin · Dec 23, 2010 at 11:51 PM 0
Share

Thanks... I did it on the run.

avatar image wenhua · Feb 03, 2012 at 03:15 AM 0
Share

any idea on how to display the distance in the scene. distance consider in km or pix.?? somethings

avatar image
1

Answer by Mickydtron · Dec 23, 2010 at 08:33 PM

Vector3.Distance(transform.position, othertransform.position); will return the distance between the position of this object and the position of the object that we have assigned othertransform to. We could use this in code (javascript) like such:

var othertransform : Transform;

function Awake() { othertransform = GameObject.FindWithTag("Target").transform; }

function Update() { if (Vector3.Distance(transform.position, othertransform.position) > 10) { DoSomething(); } }

Update is called each frame, so it will always have an accurate distance reading.

Comment
Add comment · Show 4 · 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 Jesus_Freak · Dec 23, 2010 at 08:36 PM 0
Share

there's no need to reset the Transform if you set it in the Inspector.

avatar image Mickydtron · Dec 23, 2010 at 09:10 PM 0
Share

That is true, you could set it in the Inspector. I always forget about that. Probably comes from my program$$anonymous$$g background. Dragging and dropping is too primitive. :)

avatar image Statement · Dec 23, 2010 at 11:48 PM 0
Share

@$$anonymous$$ikeydtron : http://notinventedhe.re/on/2010-12-21 :)

avatar image wenhua · Feb 03, 2012 at 03:15 AM 0
Share

any idea on how to display the distance in the scene. distance consider in km or pix.?? somethings

avatar image
1

Answer by Jesus_Freak · Dec 23, 2010 at 08:34 PM

just put a distance function in the function Update, and also do a if(distance > 10) in the function Update.

so something like

var that : Transform;
function Update()
{
 var distFromPlayer = Vector3.Distance(that.position, transform.position)
 if(distFromPlayer > 10)
 {
  //do more stuff here.
 }
}

i think might work.

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 Mickydtron · Dec 23, 2010 at 08:37 PM 0
Share

Vector3.Distance(a,b) return a float value, not another Vector3. It is just a distance, not the difference vector.

avatar image Jesus_Freak · Dec 23, 2010 at 08:40 PM 0
Share

well in that case, our answers are very similar...

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

1 Person is following this question.

avatar image

Related Questions

Decrease distance between 2 objects 3 Answers

How to get distance between camera and object? 1 Answer

Check for an instantiated object farthest from player? 1 Answer

Trigger sound on another gameobject 1 Answer

Getting vectors on an object every so many units 0 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