Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. ublic class CameraController : MonoBehaviour
  2. {
  3.     public Transform target;
  4.     public Transform cam;
  5.     public Vector3 offsetPos;
  6.     public Vector3 camRotation;
  7.     public float moveSpeed = 5;
  8.     public float turnSpeed = 10;
  9.     public float smoothSpeed = 0.02f;
  10.  
  11.     Quaternion targetRotation;
  12.     Vector3 targetPos;
  13.     Vector3 targetRot;
  14.     bool smoothRotating = false;
  15.  
  16.     void Update()
  17.     {
  18.         MoveWithTarget();
  19.         LookAtTarget();
  20.  
  21.         //if (Input.GetKeyDown(KeyCode.Q) && !smoothRotating)
  22.         //{
  23.         //StartCoroutine("RotateAroundTarget", 45);
  24.         //}
  25.  
  26.         //if (Input.GetKeyDown(KeyCode.E) && !smoothRotating)
  27.         //{
  28.         //StartCoroutine("RotateAroundTarget", -45);
  29.         //}
  30.         cam.rotation.y = target.rotation.y + 180;
  31.     }
  32.    
  33.     void MoveWithTarget()
  34.     {
  35.         targetPos = target.position + offsetPos;
  36.         transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
  37.     }
  38.  
  39.     void LookAtTarget()
  40.     {
  41.         targetRotation = Quaternion.LookRotation(target.position - transform.position);
  42.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
  43.     }
  44.  
  45.     IEnumerator RotateAroundTarget(float angle)
  46.     {
  47.         Vector3 vel = Vector3.zero;
  48.         Vector3 targetOffsetPos = Quaternion.Euler(0, angle, 0) * offsetPos;
  49.         float dist = Vector3.Distance(offsetPos, targetOffsetPos);
  50.         smoothRotating = true;
  51.  
  52.         while(dist > 0.02f)
  53.         {
  54.             offsetPos = Vector3.SmoothDamp(offsetPos, targetOffsetPos, ref vel, smoothSpeed);
  55.             dist = Vector3.Distance(offsetPos, targetOffsetPos);
  56.             yield return null;
  57.         }
  58.  
  59.         smoothRotating = false;
  60.         offsetPos = targetOffsetPos;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement