Advertisement
Muk99

CameraBehaviour

Jan 15th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraBehaviour : MonoBehaviour {
  5.    
  6.     public Transform target;
  7.     public float distance = 3.0f;
  8.     public float minDistance = 1.0f;
  9.     public float height = 2.0f;
  10.     public float smooth = 3.0f;
  11.     public float zoomSmooth = 25.0f;
  12.     public LayerMask collideLayers;
  13.  
  14.     void LateUpdate () {
  15.         //Early out if there's no target
  16.         if (!target) {
  17.             Debug.LogWarning ("The camera behaviour script doesn't have an target to follow");
  18.             return;
  19.         }
  20.  
  21.         //Calculate the position to go if there's nothing in front of camera
  22.         Vector3 wantedPos = target.position;
  23.         wantedPos = target.forward * (-distance) + wantedPos;
  24.         wantedPos.y = target.position.y + height;
  25.  
  26.         //Check if there is something in front of camera and return hit info
  27.         RaycastHit hit = new RaycastHit ();
  28.         bool isColliding = Physics.Linecast (target.position,
  29.                                              wantedPos,
  30.                                              out hit,
  31.                                              collideLayers);
  32.  
  33.         //Add a little less distance from collision point to avoid camera entering meshes
  34.         hit.point += transform.TransformDirection (Vector3.forward) * 0.05f;
  35.  
  36.         //If there isn't anything in front of camera, then go to the calculate position
  37.         if (!isColliding) {
  38.             transform.position = Vector3.Slerp (transform.position,
  39.                                                 wantedPos,
  40.                                                 smooth * Time.deltaTime);
  41.         }
  42.  
  43.         //Else if there is and the hit distance is grater than min distance, go to hit point
  44.         else if (hit.distance > minDistance){
  45.             transform.position = Vector3.Slerp (transform.position, hit.point ,zoomSmooth * Time.deltaTime);
  46.         }
  47.  
  48.         //Else if there's something in front of camera and the distance is smaller than hit distance,
  49.         //check if there's something above target, if there is, go to the hit point above target
  50.         //else go (min distance * 2) up
  51.         else {
  52.             //Create a vector3 to check the collision up
  53.             Vector3 upVector = target.position;
  54.             upVector.y += minDistance * 2;
  55.  
  56.             isColliding = Physics.Linecast (target.position,
  57.                                             upVector,
  58.                                             out hit,
  59.                                             collideLayers);
  60.  
  61.             hit.point -= transform.TransformDirection (Vector3.forward) * 0.12f;
  62.  
  63.             if (isColliding)
  64.                 transform.position = Vector3.Slerp (transform.position, hit.point ,zoomSmooth * Time.deltaTime);
  65.        
  66.             else
  67.                 transform.position = Vector3.Slerp (transform.position, upVector ,zoomSmooth * Time.deltaTime);
  68.  
  69.             //Draw linecast on editor
  70.             Debug.DrawLine (target.position, upVector, Color.red);
  71.         }
  72.  
  73.         //Always look at target
  74.         transform.LookAt (target.position);
  75.  
  76.         //Draw linecast on editor
  77.         Debug.DrawLine (target.position, wantedPos, Color.black);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement