Advertisement
Guest User

thirdPersonCameraHelper.cs

a guest
Jan 4th, 2014
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public static class thirdPersonCameraHelper
  4. {
  5.     public struct ClipPlanePoints
  6.     {
  7.         public Vector3 upperLeft;
  8.         public Vector3 upperRight;
  9.         public Vector3 lowerLeft;
  10.         public Vector3 lowerRight;
  11.     }
  12.  
  13.     // Clamp angle to between -360, 360.
  14.     public static float clampingAngle(float angle, float min, float max)
  15.     {
  16.         do
  17.         {
  18.             if (angle < -360)
  19.                 angle += 360;
  20.             if (angle > 360)
  21.                 angle -= 360;
  22.         } while (angle < -360 || angle > 360);
  23.  
  24.  
  25.  
  26.         return Mathf.Clamp (angle, min, max);
  27.     }
  28.  
  29.     public static ClipPlanePoints ClipPlaneAtNear (Vector3 pos)
  30.     {
  31.         ClipPlanePoints clipPlanePoints = new ClipPlanePoints ();
  32.  
  33.         // Ensure there is a main camera for us to check.
  34.         if (!Camera.main)
  35.             return clipPlanePoints;
  36.  
  37.         // Properties to determine the main camera's near clip plane.
  38.         Transform mainCameraTransform = Camera.main.transform;
  39.         float halfFieldOfView = (Camera.main.fieldOfView / 2) * Mathf.Deg2Rad;
  40.         float aspect = Camera.main.aspect;
  41.         float distance = Camera.main.nearClipPlane;
  42.         float height = distance * Mathf.Tan (halfFieldOfView);
  43.         float width = height * aspect;
  44.  
  45.         clipPlanePoints.lowerRight = pos + mainCameraTransform.right * width;
  46.         clipPlanePoints.lowerRight -= mainCameraTransform.up * height;
  47.         clipPlanePoints.lowerRight += mainCameraTransform.forward * distance;
  48.  
  49.         clipPlanePoints.lowerLeft = pos - mainCameraTransform.right * width;
  50.         clipPlanePoints.lowerLeft -= mainCameraTransform.up * height;
  51.         clipPlanePoints.lowerLeft += mainCameraTransform.forward * distance;
  52.  
  53.         clipPlanePoints.upperRight = pos + mainCameraTransform.right * width;
  54.         clipPlanePoints.upperRight += mainCameraTransform.up * height;
  55.         clipPlanePoints.upperRight += mainCameraTransform.forward * distance;
  56.  
  57.         clipPlanePoints.upperLeft = pos - mainCameraTransform.right * width;
  58.         clipPlanePoints.upperLeft += mainCameraTransform.up * height;
  59.         clipPlanePoints.upperLeft += mainCameraTransform.forward * distance;
  60.  
  61.         return clipPlanePoints;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement