Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Measure a distance point to point

Discussion in 'Scripting' started by foufrix, Aug 2, 2016.

  1. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    Hello everybody, I have a scene where there are some object and i can move on the scene on x, y and z, and i want to be able to make measure on an object.

    I know that to get a measure i have to use Vector3.Distance, but i have nlo clue how to put two points on an object ?

    I think i have to use Input.MousePosition to get the position, but i dont know who to create the point on an object.

    I thank you in advance for the community help, i'm new in coding so maybe i miss something obvious ..
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    How does the object move? what do you want to measure?
     
  3. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    The objects don't move.

    I'm the only one who can move in the scene and in fly mode. For example imagine their is a bridge, i want to be able to put a marker on a side of the bridge and then put another marker on the other, and be able to measure the distance between those 2 points.

    Tell me if it is not clear :)
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Do you want to place the markers on a mouse click? then store these two in a vector3.distance to see?
     
  5. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    Yes i think it s that ! i would like to place the first marker with Input.GetMouseButtonDown(0) and the second with Input.GetMouseButtonDown(1), and when it's done it will show the distance between the two points
    Thank you very much for your time !
     
  6. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    You can do this using raycasts, and using conditions to check
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class checkDistance : MonoBehaviour
    5. {
    6.     public Camera levelCamera; // the level camera/main camera
    7.     private bool firstPoint = false;
    8.     private bool secondPoint = false;
    9.  
    10.     public Transform firstTransform; // just an empty gameobject for these
    11.     public Transform secondTransform;
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         Ray ray = levelCamera.ScreenPointToRay(Input.mousePosition); // gets mouse position on a floor/object
    23.         RaycastHit hit;
    24.  
    25.         if (Physics.Raycast(ray,out hit))
    26.         {
    27.             if (Input.GetMouseButtonDown(0) && !firstPoint)
    28.             {
    29.                 firstTransform.position = hit.point; // hit.point is the current mouse position
    30.                 firstPoint = true;
    31.             }
    32.             if (Input.GetMouseButtonDown(1) && !secondPoint)
    33.             {
    34.                 secondTransform.position = hit.point;
    35.                 secondPoint = true;
    36.             }
    37.         }
    38.  
    39.         if (firstPoint && secondPoint) // when both conditions are true, print out the distance
    40.         {
    41.             float distance = Vector3.Distance(firstTransform.position, secondTransform.position);
    42.             print("distance" + distance); // if you want to display thedistance on screen, use UI Text
    43.         }
    44.  
    45.     }
    46. }
    47.  
     
    foufrix likes this.
  7. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    Wow thank you for this fast reply !!!

    I tried it and i dont know why but when i click nothing happen. I ve put this script on my main camera and i have selected it in level camera.

    I put this :

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0) && !firstPoint)
    2.             {
    3.                 firstTransform.position = hit.point; // hit.point is the current mouse position
    4.                 firstPoint = true;
    5.                 Debug.Log ("First Click ok");
    6.             }
    in order to see if the first and the second marker are placed but nothing happened

    I think i m doing something wrong because your code seems ok for me
     
  8. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @foufrix
    I've tested this code, it works. Here is a check list:
    • Make sure you have colliders on the objects you want to click on.
    • Assign the camera.
    • Assign the empty objects to first and second object transform. (suggestion: used spheres without colliders to visualized where the measuring points are).
    • left click to select first point.
    • right click to select second point. (Took some time to figure that out, @Rob21894 you should have told us a bit more about your script).
     
    foufrix likes this.
  9. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    Ok i ll try this and come back to you.

    For the last point, for his defense it's me who written it first and right click on the post, don't blame poor rob ! :D
     
  10. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @foufrix Oh, yeah! That's actually your own requirements. I've completely missed that... :p sorry @Rob21894.
     
  11. foufrix

    foufrix

    Joined:
    Jun 27, 2016
    Posts:
    6
    It perfectly works :) i forget to add an empty object to the transform. Thank you very much @Rob21894 for the code and @ericbegue for the interpretation.

    I wish you too a very good day !
     
  12. lulitha

    lulitha

    Joined:
    Feb 8, 2017
    Posts:
    9
    i am ray casting 4 points. how do i measure?