How do I use raycasts to see coordinates of the hit?

I want to click on a plane, and get the coordinates of the point on the plane that I clicked.

Take a look at the [Barycentric Coordinates][1]. Alternatively you can just use the [Point][2] parameter and calculate it yourself, knowing the size of the plane, it's orientation, and it's position in the world. [1]:http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-barycentricCoordinate.html [2]:http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-point.html

2 Answers

2

If you are using Unity Script then you can do it like this:

 var hit : RaycastHit;
 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, 1000) {
     var localHit = transform.InverseTransformPoint(hit.point);
     //Do something with the local coordinates
 }

This will give you the hit point in terms of the plane’s coordinate system.

You know, I never use InverseTransform. I should start using it.

You need to do a regular ray cast. This will give you the position on the plane you clicked on with your mouse in world coordinates.

You can also use Camera.main.ScreenPointToRay() like here: http://answers.unity3d.com/questions/13577/using-raycast-to-get-mouse-input.html