- Home /
Question by
DevMerlin · Jan 03, 2015 at 05:22 PM ·
screenmousepositionray
How can I detect if non-GUI interaction is on the upper or lower portion of the screen?
I have a working GUI, but the main portion of my gameplay comes from interaction with the screen. The player can click / tap on either the upper or lower sections of a screen split in half in order to trigger basic commands. This is NOT intended to send a ray into the world from the action position - that is handled by another section of code.
So far, I have attempted to use a plane and calculate through basic Input.mousePosition, although the latter only accounts for mouse, not touch. This is what I have come up with, but it doesn't really work well. I've determined that my Plane is not being hit for some reason.
Yes, it uses ScreenPointToRay, but primarily to try to hit the plane:
private void Click()
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
}
float distance;
if (HitPlane.Raycast(ray, out distance))
{
RawRayPosition = ray.GetPoint(distance);
Debug.Log (ray.GetPoint(distance));
ClickedPosition = new Vector3(RawRayPosition.x, RawRayPosition.y, 0);
}
}
Comment