_Ziens_

Unity - Camera Shift By Borders (Desktop)

Nov 5th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. //Global variables
  2.     private const float STRAFE_SCALE = 0.1f;
  3.     private Rect movingRect;
  4.     private const float MAX_DISTANCE = 1400f;
  5.     float distance = 200f;
  6.  
  7.     void Start ()
  8.         {
  9.             float w = Screen.width * 0.6f;
  10.             float h = Screen.height * 0.7f;
  11.  
  12.             movingRect = new Rect ((Screen.width - w) / 2f, (Screen.height - h) / 2f, w, h);
  13.             //...  
  14.         }
  15.  
  16.     void Update ()
  17.         {
  18.                 Quaternion rotation = Quaternion.Euler (40f, rotationX, 0);
  19.  
  20.               float movingX = 0.0f;
  21.                 float movingY = 0.0f;
  22.                 Vector3 mouse = Input.mousePosition;
  23.  
  24.                 //Bound Camera Strafe
  25.                 if (mouse.x > movingRect.xMax)
  26.                     movingX += STRAFE_SCALE * (mouse.x - movingRect.xMax);
  27.                 else if (mouse.x < movingRect.xMin)
  28.                     movingX += STRAFE_SCALE * (mouse.x - movingRect.xMin);
  29.                 else
  30.                     movingX = 0;
  31.  
  32.                 if (mouse.y > movingRect.yMax)
  33.                     movingY += STRAFE_SCALE * (mouse.y - movingRect.yMax);
  34.                 else if (mouse.y < movingRect.yMin)
  35.                     movingY += STRAFE_SCALE * (mouse.y - movingRect.yMin);
  36.                 else
  37.                     movingY = 0;
  38.  
  39.                 tran += rotation * new Vector3 (movingX, 0, movingY);
  40.                 tran.y = 0f;
  41.                 tran.x = Mathf.Clamp (tran.x, -MAX_DISTANCE, MAX_DISTANCE);
  42.                 tran.z = Mathf.Clamp (tran.z, -MAX_DISTANCE, MAX_DISTANCE);
  43.  
  44.                 //...
  45.                
  46.                 Vector3 dist = rotation * new Vector3 (0, 0, -distance);
  47.  
  48.                 transform.rotation = rotation;
  49.                 transform.position = GetSmoothedHeightAt (tran) + dist;
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment