Advertisement
Purianite

Untitled

Mar 22nd, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace UnityStandardAssets.Utility
  5. {
  6.  
  7.     public class FollowTarget : MonoBehaviour
  8.     {
  9.         /*
  10.         This camera smoothes out rotation around the y-axis and height.
  11.         Horizontal Distance to the target is always fixed.
  12.    
  13.         There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
  14.    
  15.         For every of those smoothed values we calculate the wanted value and the current value.
  16.         Then we smooth it using the Lerp function.
  17.         Then we apply the smoothed values to the transform's position.
  18.         */
  19.  
  20.         // The target we are following
  21.         public Transform target;
  22.         // The distance in the x-z plane to the target
  23.         public float distance = 10.0f;
  24.         // the height we want the camera to be above the target
  25.         public float height = 5.0f;
  26.         // How much we
  27.         public float heightDamping = 2.0f;
  28.         public float rotationDamping = 3.0f;
  29.  
  30.         void  LateUpdate (){
  31.             // Early out if we don't have a target
  32.             if (!target)
  33.                 return;
  34.  
  35.             // Calculate the current rotation angles
  36.             float wantedRotationAngle = target.eulerAngles.y;
  37.             float wantedHeight = target.position.y + height;
  38.  
  39.             float currentRotationAngle = transform.eulerAngles.y;
  40.             float currentHeight = transform.position.y;
  41.  
  42.             // Damp the rotation around the y-axis
  43.             currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
  44.  
  45.             // Damp the height
  46.             currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  47.  
  48.             // Convert the angle into a rotation
  49.             Quaternion currentRotation= Quaternion.Euler (0, currentRotationAngle, 0);
  50.  
  51.             // Set the position of the camera on the x-z plane to:
  52.             // distance meters behind the target
  53.             transform.position = target.position;
  54.             transform.position -= currentRotation * Vector3.forward * distance;
  55.  
  56.             // Set the height of the camera
  57.             transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
  58.  
  59.             // Always look at the target
  60.             transform.LookAt (target);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement