Raycast Dead-Zone

Hi guys,

I’ve been working on getting an interactive map into my project for the last couple of days, and have managed to get it doing what I want for the most part. It works by drawing an orthographic camera view over the main view with the Normalized View Ports control. The User can then click on the visible terrain in the map and center the camera over it. Here’s the script:

var overviewCamera : Camera;

function Update() {
   var destpoint : Vector3;
   var hit : RaycastHit;
   var movePos : Vector3;
      if (Input.GetMouseButtonDown(0))
      {
         var ray = overviewCamera.ScreenPointToRay (Input.mousePosition); // mouse pointer to game world

         if (Physics.Raycast (ray, hit, 600)) // See if I actually clicked on something nearby
          {
			destpoint = hit.point;
            movePos = Vector3(destpoint.x, transform.position.y, destpoint.z);
			gameObject.transform.position = movePos;
			gameObject.GetComponent("ZoomOverview").cameraPanSideValue = movePos.x;
			gameObject.GetComponent("ZoomOverview").cameraPanVertValue = movePos.z;
         }
      }
   }

My problem is, because the scene is bigger than the camera view, there are sections of the landscape not visible to the camera in its initial position. Because of this, if you happen to click OUTSIDE of the camera view port, and there is a collide-able surface there (from the Map Camera’s Viewpoint) then the Raycast is still registered and the map moves. What I want to know is if it is possible for me to restrict all mouse-click Raycast’s to ONLY inside the Map Camera’s view port.

I’ve attached some images to help



Isn’t there some setting for colliders to ignore raycasts? (Sorry if I’m a bit vague, can’t quite remember the details)

You can use Camera.ScreenToViewportPoint to get the screen position in viewport coordinates. For the visible view, the coordinates go from 0 to 1. A screen coordinate outside the view will give a viewport coordinate outside the 0…1 range and you can check for this happening:-

var vRect: Rect;

function Start() {
  vRect = Rect(0, 0, 1, 1);
}
   ...

var vCoord = overviewCamera.ScreenToViewportPoint(Input.mousePosition);

if (vRect.Contains(vCoord)) {
    // Point is within the map view.
}