X/Y/Z distance

Hi Guys n Girls,

I’m making a measurement tool n I had a couple of questions,

  1. has anyone got any ideas on how I could get the X/Y/Z measurements in a similar way?
  2. is their a simple way to connect the 2 “dist” objects with a line render??

Code:

 public class Measure : MonoBehaviour {
 
     public GameObject Measure1;
     public GameObject Measure2;
     public Text label;
     public float Distance_;
     //public float Distance_x;
     //public float Distance_y;
     //public float Distance_z;
 
     // Use this for initialization
     void Start () {
     }
 
     // Update is called once per frame
     void Update ()
     {
         Distance_ = Vector3.Distance (Measure1.transform.position, Measure2.transform.position);
         label.text = Distance_.ToString ("F2");
     }
 }

The distance between two “X” coordinates is merely subtraction.

Distance_x = Mathf.Abs( Measure1.transform.position.x - Measure2.transform.position.x );

Repeat for Y and Z, and ignore Abs if you want negative values ( to represent direction, though that’s unlikely )

However, you might prefer to change your member storage strategy and try

Distance_v = Measure1.transform.position - Measure2.transform.position;

Where Distance_v is a Vector3, not 3 floats. Then, if you only want positive values, read the values of x, y and z from Distance_v with Mathf.Abs().

While this imposes the potential of negative results, it has one advantage. Distance_v.magnitude replaces your call for distance between two vectors.

Wow, that works perfectly, thanks!