_Ziens_

Unity - Camera Shift By Borders (Android)

Nov 5th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 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.                 if (Input.touchCount == 0)
  21.                     return;
  22.                 Touch t = Input.GetTouch (0);
  23.                 float movingX = 0.0f;
  24.                 float movingY = 0.0f;
  25.                 Vector3 mouse = new Vector3 (t.position.x, t.position.y);
  26.  
  27.                 if (mouse.x > movingRect.xMax)
  28.                     movingX += STRAFE_SCALE * (mouse.x - movingRect.xMax);
  29.                 else if (mouse.x < movingRect.xMin)
  30.                     movingX += STRAFE_SCALE * (mouse.x - movingRect.xMin);
  31.                 else
  32.                     movingX = 0;
  33.  
  34.                 if (mouse.y > movingRect.yMax)
  35.                     movingY += STRAFE_SCALE * (mouse.y - movingRect.yMax);
  36.                 else if (mouse.y < movingRect.yMin)
  37.                     movingY += STRAFE_SCALE * (mouse.y - movingRect.yMin);
  38.                 else
  39.                     movingY = 0;
  40.  
  41.                 tran += rotation * new Vector3 (movingX, 0, movingY);
  42.                 tran.y = 0f;
  43.                 tran.x = Mathf.Clamp (tran.x, -MAX_DISTANCE, MAX_DISTANCE);
  44.                 tran.z = Mathf.Clamp (tran.z, -MAX_DISTANCE, MAX_DISTANCE);
  45.  
  46.                 //...
  47.                
  48.                 Vector3 dist = rotation * new Vector3 (0, 0, -distance);
  49.  
  50.                 transform.rotation = rotation;
  51.                 transform.position = GetSmoothedHeightAt (tran) + dist;
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment