Advertisement
LostMystic

Chase Camera Script

Jan 29th, 2014
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. /// <summary>
  2. /// Simple Chase Camera Script
  3. ///
  4. /// Follows the player but stays in the exact same facing.
  5. /// Attach this to the main camera object, and enter the name of the player in goLookAt
  6. /// in the inspector.
  7. ///
  8. /// You may use this script for commercial purposes
  9. ///
  10. /// http://lostmystic.com
  11. /// Written in Unity 4.3.3
  12. /// </summary>
  13. using UnityEngine;
  14. using System.Collections;
  15.  
  16. public class CamChaseFixed : MonoBehaviour {
  17.  
  18.     // How far away the camera will be, max from the player
  19.     public float fDistance = 8f;
  20.     public GameObject goLookAt;
  21.     public float smooth = 10f;
  22.     public Vector3 vOffset = new Vector3(0,1,0);
  23.    
  24.  
  25.     // Update is called once per frame
  26.     void Update () {
  27.        
  28.         float dist = Vector3.Distance(transform.position, goLookAt.transform.position);
  29.  
  30.         Vector3 vLook = goLookAt.transform.position + vOffset;
  31.  
  32.         // Reverse vector and extend into distance
  33.         Vector3 vCamPos = goLookAt.transform.TransformDirection(Vector3.forward);
  34.         vCamPos *= -1;
  35.         vCamPos *= fDistance;
  36.         vCamPos += goLookAt.transform.position;
  37.  
  38.         // If lower than straight on, then simply look straight
  39.         if (vCamPos.y < vLook.y) vCamPos.y = vLook.y;
  40.        
  41.         transform.position = vCamPos;
  42.  
  43.         // Rotates the camera to look at player
  44.         transform.LookAt(vLook);
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement