- Home /
Mouse plane does not detect height
the basic check nearly everyone does for raycasting mouse clicks is something like this:
if (Input.GetMouseButton(1))
{
Plane plane = new Plane(Vector3.up, transform.position);
rayToMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(rayToMouse, out dist);
Vector3 targetPos = rayToMouse.GetPoint(dist);
if (Physics.Raycast(rayToMouse, out hitTag, layerMask))
{
//if (hitTag.transform.name != "" || hitTag.transform.gameObject.layer == 9)//9 = Ground Layer
//{
MarkerGO.transform.position = new Vector3(targetPos.x, targetPos.y - 1, targetPos.z);
//}
MovePlayerToDestination();
}
}
However in my case I need it to detect height here is a screenshot of the issue. The marker goes under the higher up walkable building even with a layermask...I think the main issue is there is no way to get a layermask for the plane. I can't figure out a way to get the plane to move up when the mouse is on a higher point.
Answer by lassade · Sep 18, 2015 at 12:16 AM
Check if the brown object is in the layerMask and have a mesh collider, for test use layerMask = -1 (everything).
Try this:
if (Input.GetMouseButton(1))
{
Plane plane = new Plane(Vector3.up, transform.position);
rayToMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
// you do not need this ! becouse hitTag is a RaycastHit and gives you this info
// float dist;
// plane.Raycast(rayToMouse, out dist);
// Vector3 targetPos = rayToMouse.GetPoint(dist);
if (Physics.Raycast(rayToMouse, out hitTag, layerMask))
{
Vector3 targetPos = hitTag.point; // The impact point in world space where the ray hit the collider.
//if (hitTag.transform.name != "" || hitTag.transform.gameObject.layer == 9)//9 = Ground Layer
//{
MarkerGO.transform.position = new Vector3(targetPos.x, targetPos.y - 1, targetPos.z);
//}
MovePlayerToDestination();
}
}
commented out plane because it doesn't seem to be needed, interesting. This did create a new problem though. For some reason even with the layermask when I tell the units to move where they are the waypoint goes on their head even though they are not part of the layermask..
It looks like this is because raycasting even with a layermask is over written by collisions...