- Home /
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
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.
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
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
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;
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
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.
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
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());
}
}
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 ?
Your answer
Follow this Question
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