How to compare two axes from Vector3?

I have two vars, both updating. They are the mouse position on the screen (Input.mousePosition) and a GameObject’s position on screen (Camera.WorldToScreenPoint).
I want to measure the distance between these points at any given time, so that I can make an action occur (in this case, GUI info appears. So, If the mouse gets within a certain distance of the gameobject on the screen, the info on that object will appear above it in GUI. My problem is that both vars (the mouse position and the gameobject position) appear as Vector3. Since the Z axes is different on both, it messes up the script. Is there any way that I can remove the Z axis and just deal with the vars in Vector2.Distance??

Javascript only, please

No raycasting suggestions

Thanks

1 Answer

1

You can simply assign the difference to a Vector2:

var v2 : Vector2 = Input.mousePosition - objectScreenPosition;
var dist = v2.magnitude;

You could also just set the ‘z’ parameter to 0:

objectScreenPosition.z = 0.0;
var dist = (Input.mousePosition - objectScreenPosition).magnitude;