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
0
Question by Jadrus · Jan 31, 2014 at 04:11 PM · runtimemeasure

Is there a tool to measure a distance in runtime?

I would give the possibility to the player at any time to measure the distance between two points

Comment
Add comment · Show 3
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 robertbu · Jan 31, 2014 at 04:15 PM 0
Share

There is no tool. Unity is a 3D app, so what you are asking is similar to pointing at two object out your window and asking the distance. If one is a mountain far away and the other is a tree in the yard, the distance would be huge even though your finger only traveled a short distance. Unless all the object are on a plane parallel to the camera, you will need some form of Raycasting to identify the objects.

avatar image Jadrus · Jan 31, 2014 at 06:01 PM 0
Share

points are deter$$anonymous$$ed by the player. for example: the distance between a door and a window, or between a tree and a house. During the game, at runtime, I want a tool to measure the distance or size of an object

avatar image Jadrus · Jan 31, 2014 at 06:02 PM 0
Share

points are deter$$anonymous$$ed by the player. for example: the distance between a door and a window, or between a tree and a house. During the game, at runtime, I want a tool to measure the distance or size of an object

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by supernat · Jan 31, 2014 at 04:15 PM

You could add this line to your script:

 Vector3 dist = point2 - point1;
 float range = dist.magnitude;
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 Loius · Jan 31, 2014 at 04:16 PM 0
Share

And dist.magnitude is the distance in units.

avatar image Jadrus · Jan 31, 2014 at 06:01 PM 0
Share

points are deter$$anonymous$$ed by the player. for example: the distance between a door and a window, or between a tree and a house. During the game, at runtime, I want a tool to measure the distance or size of an object

avatar image
0

Answer by Headworker · Jan 31, 2014 at 04:16 PM

Well this is really simple, depending on how you get the point data:

If the points are known its as simple as that:

float range = (EndPoint - StartingPoint).magnitude // EndPoint and StartingPoint are Vector3s.

If dont know the points yet please ask with a comment.

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 Jadrus · Jan 31, 2014 at 06:01 PM 0
Share

points are deter$$anonymous$$ed by the player. for example: the distance between a door and a window, or between a tree and a house. During the game, at runtime, I want a tool to measure the distance or size of an object

avatar image
0

Answer by robertbu · Jan 31, 2014 at 06:16 PM

You have to Raycast to find the positions in a 3D world. Here is a bit of starter code. Attach it to any game object in the scene. It displays the current distance in the upper left corner. You likely want to add a line renderer to show the line being dragged.

 #pragma strict
 
 private var measuring = false;
 private var startPoint : Vector3;
 private var dist;
 
 function Update() {
     var hit : RaycastHit;
     if (Input.GetMouseButtonDown(0)) {
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
             measuring = true;
             startPoint = hit.point;
         }
     }
     
     if (measuring && Input.GetMouseButton(0)) {
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
             dist = Vector3.Distance(startPoint, hit.point);
         }
     }
 
     if (measuring && Input.GetMouseButtonUp(0)) {
         measuring = false;
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
             dist = Vector3.Distance(startPoint, hit.point);
             Debug.Log("Final distance = "+dist);
         }
     }
 }
 
 function OnGUI() {
     if (measuring) {
         GUI.Label(Rect(50,50,100,50), dist.ToString());
     }
 }
     
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 j_campbell · Feb 07, 2015 at 11:18 AM

Hi - I would like to tweak this code so that the user wouldn't have to click on the object in order to display the distance. (I want to display the distance that a 3d character runs from one part of the screen to the other). The start point will always be 0 but I would like to know what value/parameter to put in instead of 'hit.point' in order to determine the real time position of the character. Is this possible ?

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

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

23 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

Related Questions

How to make measurement markers show up, and at scale, in a large scene? 0 Answers

Export objects to a .3DS file at runtime 1 Answer

Copy collider Component type and parameter to a new Object at runtime 0 Answers

Problem adding component to object at runtime 1 Answer

generate GUIText at runtime C# 3 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