Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- namespace JasonStorey.Cameras
- {
- public abstract class SmartCamera : MonoBehaviour
- {
- [Range(0,4)]
- public float GizmoLines = 0.5f;
- [Space]
- public CameraEvent CameraActivated;
- public CameraEvent CameraDeactivated;
- [Space]
- public TransformEvent TargetFound;
- public TransformEvent TargetLost;
- private void Start() => IdleForward = CameraRoot.forward;
- #region Properties
- public Vector3 Position => transform.position;
- public virtual string Name => name;
- public virtual bool IsLive { get; }
- public virtual void Activate() => CameraActivated?.Invoke(this);
- public virtual void Deactivate() => CameraDeactivated?.Invoke(this);
- protected virtual Transform CameraRoot => transform;
- public Vector3 LookDirection => CameraRoot.forward;
- public virtual Transform LookTarget => null;
- #endregion
- #region Methods
- public bool HasUnobstructedViewOf(Collider col)
- {
- var t = transform;
- var p = t.position;
- var closestPoint = col.ClosestPoint(p);
- var dir = closestPoint - p;
- dir.Normalize();
- if (Physics.Raycast(p, dir, out var hit))
- return hit.collider == col;
- return false;
- }
- [ContextMenu("Look Forward")]
- public virtual void LookForward()
- {
- ClearWatchTarget();
- LookInDirection(IdleForward);
- }
- public float AngleQuality(Vector3 target)
- {
- var t = transform;
- var p = t.position;
- var dir = (target - p).normalized;
- return Vector3.Dot(IdleForward, dir);
- }
- public abstract void ClearWatchTarget();
- public abstract void Watch(Transform target);
- public virtual void LookAt(Transform target) => LookAt(target.position);
- public virtual void LookAt(Vector3 target)
- {
- var t = transform;
- var dir = (target - t.position).normalized;
- t.forward = dir;
- }
- #endregion
- #region Plumbing
- public void DebugDraw()
- {
- var t = CameraRoot;
- var p = t.position;
- Gizmo.DrawLine(p,p+IdleForward*GizmoLines, Color.green,2);
- Gizmo.DrawLine(p,p+t.forward*GizmoLines,Color.yellow,2);
- }
- private void LookInDirection(Vector3 direction)
- {
- CameraRoot.forward = direction;
- }
- protected Vector3 IdleForward;
- public override string ToString() => Name;
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement