Advertisement
asteroidsteam

UnityCamSmoothMove

Feb 2nd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class CamMove : MonoBehaviour
  4. {
  5.     public Transform target;
  6.     public Transform start;
  7.  
  8.     public float smoothMoveSpeed = 0.03f;
  9.     public float smoothRotateSpeed = 0.125f;
  10.     public Vector3 targetOffset;
  11.     public Vector3 startOffset;
  12.     private bool goto_target;
  13.     private bool goback;
  14.  
  15.     public bool autoFaceTarget = true;
  16.  
  17.     private int state;
  18.  
  19.     public void goToTarget()
  20.     {
  21.         goto_target = true;
  22.         goback = false;
  23.     }
  24.  
  25.     public void goBack()
  26.     {
  27.         goto_target = false;
  28.         goback = true;
  29.     }
  30.  
  31.     public int getState()
  32.     {
  33.         return state;
  34.     }
  35.  
  36.     public void toggleSmoothRotate()
  37.     {
  38.         if (autoFaceTarget)
  39.         {
  40.             autoFaceTarget = false;
  41.         }
  42.         else
  43.         {
  44.             autoFaceTarget = true;
  45.         }
  46.     }
  47.  
  48.     void FixedUpdate()
  49.     {
  50.         if (goto_target && goback == false)
  51.         {
  52.             state = 0;
  53.             Vector3 desiredPosition = target.position + targetOffset;
  54.             Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothMoveSpeed);
  55.             transform.position = smoothedPosition;
  56.             if (autoFaceTarget)
  57.             {
  58.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), smoothRotateSpeed);
  59.             }
  60.         }
  61.         else if (goback && goto_target == false)
  62.         {
  63.             state = 1;
  64.             Vector3 desiredPosition = start.position +startOffset;
  65.             Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothMoveSpeed);
  66.             transform.position = smoothedPosition;
  67.             if (autoFaceTarget)
  68.             {
  69.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(start.position - transform.position), smoothRotateSpeed);
  70.             }
  71.         }
  72.         else
  73.         {
  74.             state = 2;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement