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
1
Question by Dismortus · Aug 12, 2013 at 09:03 PM · distancefloatvector2

Get distance between 2 gameObjects?

Hi, I know how to print out the position of an object to the console. I am actually pretty good with coding, just not in Unity yet. I am used to doing small applications in Visual Studio. I simply need to know how to get the distance from one object to the other. I'd imagine I can use an if statement to make something happen if the distance is a certain range that I declare?

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

Answer by ZenithCode · Aug 12, 2013 at 09:07 PM

You can do something like this:

 public GameObject go1;
 public GameObject go2;
 
 void Awake()
 {
    if (go1 != null && go2 != null)
    {
       Vector3 dist = Vector.distance(go1.transform.position, go2.transform.position);
       Debug.Log(string.Format("Distance between {0} and {1} is: {2}", go1, go2, dist));
    }
 }

Reference: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html

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 deadfish90 · Aug 12, 2013 at 10:04 PM 2
Share

This is the easiest way to get distance between two gameObjects. I just wanted to add two things to this answer.

  1. If you want to constantly check the distance (as in you want something to happen when A is close to B), make sure to throw the function Vector.distance() into the update function. Be sure to declare the Vector3 outside of update though.

  2. If the script is attached to one of the GameObjects you are actually trying to check, you only need one public GameObject variable. ins$$anonymous$$d of go2.transform.position, you can just use this.gameObject.transform.position.

Not sure if Dismortus knew these differences or not, so I just thought I'd make mention of them.

avatar image ZenithCode · Aug 13, 2013 at 09:35 AM 2
Share

Yes I gave an overview but on how to get the distance. I totally agree with deadfish90's recommendations (if they fit your purpose)

avatar image Dismortus · Aug 13, 2013 at 06:23 PM 1
Share

Thank you very much, this was easier than I thought it would be :)

avatar image
0

Answer by Triqy · Aug 12, 2013 at 09:26 PM

You can use transform.LookAt(Target) to look at the gameobject you want to find the distance between then you could shoot a raycast at the gameobject using Vector3.Distance to find the distance

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 Triqy · Aug 12, 2013 at 09:28 PM 0
Share

just another way of doing it by shooting raycasts... lol

avatar image Dismortus · Aug 13, 2013 at 06:25 PM 0
Share

I'm sorry, I may have misread, but to "shoot a raycast at the gameObject using Vector3.Distance" doesn't seem to make sense to me. Perhaps you could reword? I am new to Unity's classes.

avatar image MantisFRK · Aug 13, 2013 at 07:30 PM 0
Share

A raycast is sending a "beam" in a direction. That beam can "collide" with other objects. Once that beam (raycast, or cast a ray) hits, you can get the distance using Vector3.Distance.

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

19 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

Related Questions

UnityEngine.Vector2 Float conversion problem 2 Answers

Why is my float not fitting into my AddForce script? 1 Answer

Add a float to a Vector3 in a direction 1 Answer

operator ^ cannot be applied to operands of type vector 2 and float 1 Answer

Physics.Raycast Help 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