Advertisement
kasru

C# rpg Camera

Jun 10th, 2015
6,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class UserCamera : MonoBehaviour {
  8.  
  9.     public Transform target;                            // Target to follow
  10.     public float targetHeight = 1.7f;                       // Vertical offset adjustment
  11.     public float distance = 12.0f;                          // Default Distance
  12.     public float offsetFromWall = 0.1f;                     // Bring camera away from any colliding objects
  13.     public float maxDistance = 20;                      // Maximum zoom Distance
  14.     public float minDistance = 0.6f;                        // Minimum zoom Distance
  15.     public float xSpeed = 200.0f;                           // Orbit speed (Left/Right)
  16.     public float ySpeed = 200.0f;                           // Orbit speed (Up/Down)
  17.     public float yMinLimit = -80;                           // Looking up limit
  18.     public float yMaxLimit = 80;                            // Looking down limit
  19.     public float zoomRate = 40;                             // Zoom Speed
  20.     public float rotationDampening = 3.0f;              // Auto Rotation speed (higher = faster)
  21.     public float zoomDampening = 5.0f;                  // Auto Zoom speed (Higher = faster)
  22.     LayerMask collisionLayers = -1;     // What the camera will collide with
  23.  
  24.     public bool lockToRearOfTarget;            
  25.     public bool allowMouseInputX = true;               
  26.     public bool allowMouseInputY = true;               
  27.    
  28.     private float xDeg = 0.0f;
  29.     private float yDeg = 0.0f;
  30.     private float currentDistance;
  31.     public float desiredDistance;
  32.     private float correctedDistance;
  33.     private bool rotateBehind;
  34.    
  35.     public GameObject userModel;
  36.     public bool inFirstPerson;
  37.        
  38.     void Start () {
  39.  
  40.         Vector3 angles = transform.eulerAngles;
  41.         xDeg = angles.x;
  42.         yDeg = angles.y;
  43.         currentDistance = distance;
  44.         desiredDistance = distance;
  45.         correctedDistance = distance;
  46.        
  47.         // Make the rigid body not change rotation
  48.         if (rigidbody)
  49.             rigidbody.freezeRotation = true;
  50.        
  51.         if (lockToRearOfTarget)
  52.             rotateBehind = true;
  53.     }
  54.    
  55.     void Update () {
  56.        
  57.         if (Input.GetAxis("Mouse ScrollWheel") < 0) {
  58.            
  59.             if(inFirstPerson == true) {
  60.                
  61.                 minDistance = 10;
  62.                 desiredDistance = 15;
  63.                 userModel.SetActive(true);
  64.                 inFirstPerson = false;
  65.             }
  66.         }
  67.        
  68.         if(desiredDistance == 10) {
  69.            
  70.             minDistance = 0;
  71.             desiredDistance = 0;
  72.             userModel.SetActive(false);
  73.             inFirstPerson = true;
  74.         }  
  75.     }
  76.    
  77.     //Only Move camera after everything else has been updated
  78.     void LateUpdate ()
  79.     {
  80.  
  81.         // Don't do anything if target is not defined
  82.         if (!target)
  83.             return;
  84.        
  85.         Vector3 vTargetOffset3;
  86.  
  87.         // If either mouse buttons are down, let the mouse govern camera position
  88.         if (GUIUtility.hotControl == 0)
  89.         {
  90.             if(Input.GetKey(KeyCode.LeftControl)) {
  91.                
  92.             }
  93.             else {
  94.                 if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  95.                 {
  96.                     //Check to see if mouse input is allowed on the axis
  97.                     if (allowMouseInputX)
  98.                         xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
  99.                     if (allowMouseInputY)
  100.                         yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
  101.  
  102.                 }
  103.             }
  104.         }
  105.         ClampAngle (yDeg);
  106.        
  107.         // Set camera rotation
  108.         Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0);
  109.        
  110.         // Calculate the desired distance
  111.         desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance);
  112.         desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance);
  113.         correctedDistance = desiredDistance;
  114.        
  115.         // Calculate desired camera position
  116.         Vector3 vTargetOffset = new Vector3 (0, -targetHeight, 0);
  117.         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset);
  118.        
  119.         // Check for collision using the true target's desired registration point as set by user using height
  120.         RaycastHit collisionHit;
  121.         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z);
  122.        
  123.         // If there was a collision, correct the camera position and calculate the corrected distance
  124.         bool isCorrected = false;
  125.         if (Physics.Linecast (trueTargetPosition, position,out collisionHit, collisionLayers))
  126.         {
  127.             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall;
  128.             isCorrected = true;
  129.         }
  130.        
  131.         // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
  132.         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
  133.        
  134.         // Keep within limits
  135.         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance);
  136.        
  137.         // Recalculate position based on the new currentDistance
  138.         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset);
  139.        
  140.         //Finally Set rotation and position of camera
  141.         transform.rotation = rotation;
  142.         transform.position = position;
  143.     }
  144.  
  145.     void ClampAngle (float angle)
  146.     {
  147.         if (angle < -360)
  148.             angle += 360;
  149.         if (angle > 360)
  150.             angle -= 360;
  151.  
  152.         yDeg = Mathf.Clamp(angle, -60, 80);
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement