Get coordinates of clicked position on Object

Hi,

I would like to ask, how i can get the coordinates of my mouse over on object where i clicked.
In example i have a cube and i clicked on one of its face, how i can get the transform.position ?
Not the cube position?

Thank you.

Using a raycast, you can use an overload which allows you to get additional information (via the ‘out RaycastHit’ parameter) about the object you hit, the position the hit occured at, the normals etc.

I’m not sure what you mean by transform.position. If you mean the point in the surface of the cube where you clicked, then you can use a raycast:

var position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(ray, out hit);
if(hit.rigidbody != null)
{
    Debug.Log ("hit: " + hit.point);
}
1 Like

But there’s missing the part of the creating the ray or simply use Camera.main.ScreenPointToRay in order to initialize the ray, that should work aswell.

1 Like