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 /
  • Help Room /
avatar image
0
Question by Topthink · Nov 26, 2016 at 12:34 AM · vector3distancesyntax

Distance Between Objects

I'm trying to determine the distance between two objects. The two objects are a blue sphere named "GoodGuy" and a red sphere named "BadGuy".

I know the formula is ... distance = Vector3.Distance(obj1.position, obj2.position) ... no problem with that ... I've been researching it and I found that formula on this site.

Here is what I don't understand ... how do I fill in the syntax on obj1 and obj2 ??? I'm trying all sorts of stuff here and can't seem to get it. Here's my code:

 private float distance;
 private Vector3 obj1;   //  This is location of GoodGuy
 private Vector3 obj2;   //  This is location of BadGuy

 void Start ()
 {
     obj1 = Vector3.zero;   //  What do I put here for vector3 location?
     obj2 = Vector3.zero;   //  And Here?

     distance = Vector3.Distance (obj1, obj2);

     Debug.Log (distance);
 }    
Comment
Add comment · Show 2
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 Topthink · Nov 26, 2016 at 12:59 AM 0
Share

I'm trying to figure all this out by looking in the manual but it doesn't always show easily understandable examples.

avatar image Topthink · Nov 26, 2016 at 01:16 AM 0
Share

I want to figure out the distance between the two spheres GoodGuy and BadGuy.

5 Replies

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

Answer by Eric5h5 · Nov 26, 2016 at 03:42 AM

 Vector3.Distance (obj1.position, obj2.position)

Where obj1 and obj2 are Transforms.

Comment
Add comment · Show 7 · 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 Topthink · Nov 26, 2016 at 04:22 AM 0
Share

Thanks. I understand that. I just can't figure out how to write it up so it doesn't give me an error of some sort. I guess I'm really struggling with the syntax issue.

avatar image Topthink Topthink · Nov 26, 2016 at 04:30 AM 0
Share

$$anonymous$$aybe I'm not asking it properly. I know I have two transforms I'm working with here. I just don't know really what to call the transform of the GoodGuy sphere and I don't know what to call the transform of the BadGuy sphere.

For example, is it the "GoodGuy.transform" or could it be the "this.Badguy.something.somethinElse.transform.position"

I'm trying to find examples here on the website that shows this but I'm having a hard time finding something that seems right on point.

avatar image Eric5h5 Topthink · Nov 26, 2016 at 04:56 AM 0
Share

Change the type of obj1 and obj2 from Vector3 to Transform. Get rid of the attempts at assigning values in Start. $$anonymous$$ake obj1 and obj2 public so you can assign them in the editor.

Show more comments
avatar image
0

Answer by nostalgicbear · Nov 27, 2016 at 08:27 AM

Hey so what you have is perfectly fine, and will work perfectly.

The "syntax for obj1 and obj2" will be a Vector3 (a position consisting of an "x,y,z"). It might make more sense if you saw your code like this :

  public Transform obj1;
     public Transform obj2;
     float distanceBetweenThem;
     // Use this for initialization
     void Start () {
 
     }
 
     void Update()
     {
         distanceBetweenThem = Vector3.Distance(obj1.position, obj2.position);
 
         Debug.Log("Distance between obj1 and obj2 is " + distanceBetweenThem);
     }

You are getting the distance between the position of two objects. If you were to look in the inspector, you would see the positions of the objects here:

Position

In your code, when you say "private Vector obj1" - That Vector is made up of obj1's x,y,z position. You are then checking the distance of that from obj2's x,y,z position.

So if obj1 was at (0,0,5) and obj2 was at (0,0,7), the distance would be 2.

Hope that helps.

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
avatar image
0

Answer by recagonlei · Nov 26, 2016 at 04:46 PM

Put the in inspector the two objects in their slots and put in the void Update() to update every frame:

    private float distance;
    public Transform obj1;   //  This is location of GoodGuy
    public Transform obj2;   //  This is location of BadGuy

    void Update()
    {
        distance = Vector3.Distance (obj1.position, obj2.position);
        Debug.Log (distance);
    }   
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
avatar image
0

Answer by arghyaghosh · Nov 26, 2016 at 04:37 PM

    public Transform obj1;     //  This is location of GoodGuy  -- Drag and Drop GoodGuy
    public Transform obj2;    //  This is location of BadGuy      -- Drag and Drop BadGuy
    private float distance;

      void Start ()
      {
         distance = Vector3.Distance (obj1.position , obj2.position);
         Debug.Log (distance);
      }    
Comment
Add comment · Show 1 · 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 Topthink · Nov 26, 2016 at 07:23 PM 0
Share

I'm trying to figure out how this would work real time.

avatar image
0

Answer by Topthink · Nov 26, 2016 at 07:24 PM

I appreciate everyone taking the time to help me understand this. Thanks.

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

67 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

Related Questions

Unity Vector3.Distance calculating wrong 1 Answer

Move GameObject to Point A to B 1 Answer

Problem : Scale Object by distance 0 Answers

How do I find a point along a line with a forced x and z values 0 Answers

Get vector3 from cursor position relative to object in 3D plane 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