apieceoffruit

SmartCamera

May 26th, 2021 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace JasonStorey.Cameras
  4. {
  5.     public abstract class SmartCamera : MonoBehaviour
  6.     {
  7.         [Range(0,4)]
  8.         public float GizmoLines = 0.5f;
  9.         [Space]
  10.         public CameraEvent CameraActivated;
  11.         public CameraEvent CameraDeactivated;
  12.         [Space]
  13.         public TransformEvent TargetFound;
  14.         public TransformEvent TargetLost;
  15.  
  16.         private void Start() => IdleForward = CameraRoot.forward;
  17.  
  18.         #region Properties
  19.  
  20.         public Vector3 Position => transform.position;
  21.         public virtual string Name => name;
  22.  
  23.         public virtual bool IsLive { get; }
  24.        
  25.         public virtual void Activate() => CameraActivated?.Invoke(this);
  26.         public virtual void Deactivate() => CameraDeactivated?.Invoke(this);
  27.         protected virtual Transform CameraRoot => transform;
  28.  
  29.         public Vector3 LookDirection => CameraRoot.forward;
  30.  
  31.         public virtual Transform LookTarget => null;
  32.  
  33.         #endregion
  34.  
  35.         #region Methods
  36.        
  37.         public bool HasUnobstructedViewOf(Collider col)
  38.         {
  39.             var t = transform;
  40.             var p = t.position;
  41.             var closestPoint = col.ClosestPoint(p);
  42.  
  43.             var dir = closestPoint - p;
  44.             dir.Normalize();
  45.            
  46.             if (Physics.Raycast(p, dir, out var hit))
  47.                 return hit.collider == col;
  48.             return false;
  49.         }
  50.  
  51.         [ContextMenu("Look Forward")]
  52.         public virtual void LookForward()
  53.         {
  54.             ClearWatchTarget();
  55.             LookInDirection(IdleForward);
  56.         }
  57.  
  58.         public float AngleQuality(Vector3 target)
  59.         {
  60.             var t = transform;
  61.             var p = t.position;
  62.             var dir = (target - p).normalized;
  63.            
  64.             return Vector3.Dot(IdleForward, dir);
  65.         }
  66.  
  67.         public abstract void ClearWatchTarget();
  68.  
  69.         public abstract void Watch(Transform target);
  70.  
  71.         public virtual void LookAt(Transform target) => LookAt(target.position);
  72.  
  73.         public virtual void LookAt(Vector3 target)
  74.         {
  75.             var t = transform;
  76.             var dir = (target - t.position).normalized;
  77.             t.forward = dir;
  78.         }
  79.        
  80.         #endregion
  81.  
  82.        
  83.         #region Plumbing
  84.  
  85.         public void DebugDraw()
  86.         {
  87.             var t = CameraRoot;
  88.             var p = t.position;
  89.             Gizmo.DrawLine(p,p+IdleForward*GizmoLines, Color.green,2);
  90.             Gizmo.DrawLine(p,p+t.forward*GizmoLines,Color.yellow,2);
  91.         }
  92.  
  93.         private void LookInDirection(Vector3 direction)
  94.         {
  95.             CameraRoot.forward = direction;
  96.         }
  97.  
  98.  
  99.         protected Vector3 IdleForward;
  100.         public override string ToString() => Name;
  101.  
  102.         #endregion
  103.     }
  104.  
  105. }
Add Comment
Please, Sign In to add comment