# Vector3 Less than or equal to

hi I have this code which returns the distance between 2 points as individual coordinates(x,z,y separately).

``````distancex = Vector3(transform.position.x, object.transform.position.x);
distancez = Vector2(transform.position.z, player.transform.position.z);
distancey = Vector2(transform.position.y, player.transform.position.y);
``````

but im getting errors when I use it in such cases

e.g.

`````` if(distancex < range){
``````

I need to return the distance between 2 objects and have the code register when the objects are within certain ranges.
How do I modify this to make it so?

Distance between 2 points is Vector3.distance(vector3A, vector3B), or (v3A-v3B).magnitude
Once you have this value, you can compar it with < > range

with the first one, it works but its not what im looking for, with the second one how do I type it out?

distancex = (transform.position.x - Object.transform.position.x).magnitude;
distancez = (transform.position.z - Object.transform.position.z).magnitude;
distancey = (transform.position.y - Object.transform.position.y).magnitude;

I wrote it as such, it doesn’t come up with any errors until I hit play. it doesn’t like these lines of code

``````float distance = (transform.position - Object.transform.position).magnitude;
``````

Does not seem like he want the distance between the vectors but each vector point. If you want the distance between two numbers then you should use the absoulte value written like:

| distance |

You can get this functionality from unitys math lib Mathf.Abs( value )

He said he wanted to check range/distance. In either case, there is still a quicker way to write it then

``````Vector3 a;
Vector3 b;
Vector3 d = (a - b);

// then access individual components and do Mathf.Abs on each one if need be
d.x;
d.y;
d.z;
``````