Advertisement
Guest User

CameraController

a guest
Mar 31st, 2015
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class CameraController : MonoBehaviour
  6. {
  7.  
  8.     public Transform player;
  9.     public float damping = 0.3f;
  10.     public float lookAheadFactor = 3;
  11.     public float lookAheadReturnSpeed = 0.5f;
  12.     public float lookAheadMoveThreshold = 0.1f;
  13.  
  14.     private float offsetZ;
  15.     private Vector3 lastTargetPosition;
  16.     private Vector3 currentVelocity;
  17.     private Vector3 lookAheadPos;
  18.  
  19.  
  20.     // Use this for initialization
  21.     void Start()
  22.     {
  23.         lastTargetPosition = player.position;
  24.         offsetZ = transform.position.z - player.position.z;
  25.         transform.parent = null;
  26.     }
  27.  
  28.  
  29.     public void Awake()
  30.     {
  31.         // Optional for manually changing the camera size
  32.         GetComponent<Camera>().orthographicSize = (Screen.height / 128f * 16f); // Play around with this to get
  33.     }
  34.  
  35.     void Update()
  36.     {
  37.         float xMoveDelta = player.position.x - lastTargetPosition.x;
  38.         bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
  39.  
  40.         if (updateLookAheadTarget)
  41.         {
  42.             lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
  43.         }
  44.         else
  45.         {
  46.             lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
  47.         }
  48.  
  49.         Vector3 aheadTargetPos = player.position + lookAheadPos + Vector3.forward * offsetZ;
  50.         Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
  51.  
  52.  
  53.         transform.position = newPos;
  54.         lastTargetPosition = player.position;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement