title: Spawn within field of view but always below a certain height
body: <p>I have two systems that spawn flying enemies. The first system spawns enemies within the FOV:</p>
  
  <pre><code>distanceFromPlayer = 20;
  Ray ray = Camera.main.ScreenPointToRay(new Vector3(UnityEngine.Random.Range((float)leftRange,(float)rightRange), UnityEngine.Random.Range((float)bottomRange,(float)topRange), 0));
  screenPosition = ray.GetPoint (distanceFromPlayer);
  </code></pre>
  
  <p>The second system spawns them randomly but with a max angle between enemy and ground. Look at it as if there was a half sphere with the player as center that has been topped off: the enemy can spawn on the surface of that topped sphere:</p>
  
  <pre><code>float distance = distanceFromPlayer;
  float yaw = UnityEngine.Random.Range(0,360);
  float pitch = UnityEngine.Random.Range(1,35);
  screenPosition = RotationHelper.ConvertYawPitch (Vector3.forward * distance, yaw, pitch);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public static class RotationHelper {
  
      public static Vector3 ConvertYawPitch(Vector3 vector, float yaw, float pitch)
      {
          Quaternion yawRotation = Quaternion.AngleAxis (yaw, Vector3.up);
          Vector3 yawedZAxis = yawRotation * Vector3.left;
          Quaternion pitchRotation = Quaternion.AngleAxis (pitch, yawedZAxis);
          Vector3 yawedVector = yawRotation * vector;
          Vector3 position = pitchRotation * yawedVector;
          return position;
      }
  }
  </code></pre>
  
  <p>Now, I want to have a third system for another type of enemies, which basically combines the two systems. So: I want them to spawn inside the field of view, but not above a certain hight, measured from the ground (or below a certain angle like in system two). So if you look to the sky, it should spawn the enemies below the camera's field of view.</p>
  
  <p>I've tried an approach where I make a dummy camera that moves along with the real camera, but can't look upside, but I have the feeling it affects my performance quite drastically, and it doesn't feel like the most efficient way to do it.</p>
  
  <p>Any help is much appreciated!</p>