Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraController : MonoBehaviour
  5. {
  6.     public Transform Player;
  7.  
  8.     public Vector2 Margin;
  9.     public Vector2 Smoothing;
  10.  
  11.     public BoxCollider2D Bounds;
  12.     public BoxCollider2D BossBounds;
  13.  
  14.     public Vector3
  15.         _min,
  16.         _max;
  17.  
  18.     public bool isFollowing { get; set; }
  19.  
  20.     public void Start()
  21.     {
  22.         _min = Bounds.bounds.min;
  23.         _max = Bounds.bounds.max;
  24.         isFollowing = true;
  25.        
  26.     }
  27.  
  28.     public void Update()
  29.     {
  30.         var x = transform.position.x;
  31.         var y = transform.position.y;
  32.  
  33.         if (isFollowing)
  34.         {
  35.             if (Mathf.Abs(x - Player.position.x) > Margin.x)
  36.                 x = Mathf.Lerp(x, Player.position.x, Smoothing.x * Time.deltaTime);
  37.  
  38.             if (Mathf.Abs(y - Player.position.y) > Margin.y)
  39.                 y = Mathf.Lerp(y, Player.position.y, Smoothing.y * Time.deltaTime);
  40.  
  41.         }
  42.  
  43.         var cameraHalfWidth = camera.orthographicSize * ((float) Screen.width / Screen.height);
  44.  
  45.         x = Mathf.Clamp(x, _min.x + cameraHalfWidth, _max.x - cameraHalfWidth);
  46.         y = Mathf.Clamp(y, _min.y + camera.orthographicSize, _max.y + camera.orthographicSize);
  47.  
  48.         transform.position = new Vector3(x, y, transform.position.z);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement