Advertisement
Guest User

Camera - Clipping

a guest
May 20th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. //(Created CSharp Version) 10/2010: Daniel P. Rossi (DR9885)
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class SmoothCameraWithBumper : MonoBehaviour
  7. {
  8.     [SerializeField] private Transform target = null;
  9.     [SerializeField] private float distance = 3.0f;
  10.     [SerializeField] private float height = 1.0f;
  11.     [SerializeField] private float damping = 5.0f;
  12.     [SerializeField] private bool smoothRotation = true;
  13.     [SerializeField] private float rotationDamping = 10.0f;
  14.  
  15.     [SerializeField] private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
  16.  
  17.     [SerializeField] private float bumperDistanceCheck = 2.5f; // length of bumper ray
  18.     [SerializeField] private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
  19.     [SerializeField] private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
  20.  
  21.     /// <Summary>
  22.     /// If the target moves, the camera should child the target to allow for smoother movement. DR
  23.     /// </Summary>
  24.     private void Awake()
  25.     {
  26.         camera.transform.parent = target;
  27.     }
  28.  
  29.     private void FixedUpdate()
  30.     {
  31.         Vector3 wantedPosition = target.TransformPoint(0, height, -distance);
  32.  
  33.         // check to see if there is anything behind the target
  34.         RaycastHit hit;
  35.         Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward);
  36.  
  37.         // cast the bumper ray out from rear and check to see if there is anything behind
  38.         if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
  39.             && hit.transform != target) // ignore ray-casts that hit the user. DR
  40.         {
  41.             // clamp wanted position to hit position
  42.             wantedPosition.x = hit.point.x;
  43.             wantedPosition.z = hit.point.z;
  44.             wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
  45.         }
  46.  
  47.         transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
  48.  
  49.         Vector3 lookPosition = target.TransformPoint(targetLookAtOffset);
  50.  
  51.         if (smoothRotation)
  52.         {
  53.             Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
  54.             transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
  55.         }
  56.         else
  57.             transform.rotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement