I would give the possibility to the player at any time to measure the distance between two points
You could add this line to your script:
Vector3 dist = point2 - point1;
float range = dist.magnitude;
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.
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());
}
}
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 ?