To detect a mousedown over a Collider I can use the OnMouseDown function or I can create some code using the Physics.Raycast function.
I wonder which method is preferable?
I would like to know the answer too
This is the little piece of code that I wrote to do that:
if (Input.GetMouseButton(0))
{
Vector3 pointAtCamera = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f));
Vector3 forward = Vector3.zero;
if (!Camera.main.orthographic)
{
Vector3 pointTarget = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 100.0f));
forward = pointTarget - pointAtCamera;
forward.Normalize();
}
else
forward = Camera.main.transform.forward;
RaycastHit hit;
if (Physics.Raycast(pointAtCamera - (forward * 100), forward, out hit, Mathf.Infinity, FLOOR_LAYER))
{
// Move the position of the selected object
pickPosMarker.transform.position = new Vector3(hit.point.x, 1.0f, hit.point.z);
#if DEBUG
// DEBUG
pickPos = new Vector3(hit.point.x, hit.point.y, hit.point.z);
Debug.DrawLine(pointAtCamera - (forward * 100), pointAtCamera + (forward * 100));
#endif
}
}
Hi Diego,
Appreciate your effort, but basicly what I would like to know is if there’s a performance difference between to two methods.