File size: 4,158 Bytes
ce81a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Photon.Deterministic;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Camera))]
public class CameraFollow : MonoBehaviour
{
  public Transform Target;
  public Quantum.LayerMask OcclusionQueryMask = -1;
  [SerializeField, Range(2f, 15f)]
  float distance = 5f;
  [SerializeField, Range(0.1f, 5f)]
  float scrollSpeed = 1f;
  [SerializeField, Min(0f)]
  float focusRadius = 1f;
  [SerializeField, Range(0f, 1f)]
  float focusCentering = 0.5f;

  [SerializeField, Range(1f, 360f)]
  float rotationSpeed = 90f;
  [SerializeField, Range(-30f, 89f)]
  float minVerticalAngle = -30f, maxVerticalAngle = 60f;

  Vector3 focusPoint;
  Vector2 orbitAngles = new Vector2(45f, 0f);

  private Camera _camera;
  void Awake()
  {
    //focusPoint = focus.position;
    transform.localRotation = Quaternion.Euler(orbitAngles);
    _camera = GetComponent<Camera>();
  }
  void LateUpdate()
  {
    if (Target == null) return;
    UpdateDistance();
    UpdateFocusPoint();
    Quaternion lookRotation;
    if (ManualRotation())
    {
      ConstrainAngles();
      lookRotation = Quaternion.Euler(orbitAngles);
    }
    else
    {
      lookRotation = transform.localRotation;
    }
    
    Vector3 lookDirection = lookRotation * Vector3.forward;
    Vector3 lookPosition = focusPoint - lookDirection * distance;

    if (Cast(focusPoint, lookRotation, -lookDirection, out var hitDistance, distance - _camera.nearClipPlane))
    {
      lookPosition = focusPoint - lookDirection * (hitDistance + _camera.nearClipPlane);
    }

    transform.SetPositionAndRotation(lookPosition, lookRotation);
  }

  private unsafe bool Cast(Vector3 origin, Quaternion rotation, Vector3 direction, out float hitDistance, float distance)
  {
    hitDistance = 0;

    var frame = QuantumRunner.Default?.Game?.Frames.Verified;
    if (frame != null)
    {
      var shape = Quantum.Shape3D.CreateBox(CameraPlaneExtends.ToFPVector3());
      var options = Quantum.QueryOptions.ComputeDetailedInfo | Quantum.QueryOptions.HitAll;
      var hit = frame.Physics3D.ShapeCast(origin.ToFPVector3(), rotation.ToFPQuaternion(), &shape, (direction * distance).ToFPVector3(), OcclusionQueryMask, options);
      if (hit.HasValue)
      {
        hitDistance = (origin - hit.Value.Point.ToUnityVector3()).magnitude;
        return true;
      }
    }
    return false;
  }

  Vector3 CameraPlaneExtends
  {
    get
    {
      Vector3 halfExtends;
      halfExtends.y =
        _camera.nearClipPlane *
        Mathf.Tan(0.5f * Mathf.Deg2Rad * _camera.fieldOfView) * 1.25f;
      halfExtends.x = halfExtends.y * _camera.aspect;
      halfExtends.z = 0.01f;
      return halfExtends;
    }
  }

  void UpdateDistance()
  {
    distance += Input.GetAxisRaw("Mouse ScrollWheel") * scrollSpeed;
    distance = Mathf.Clamp(distance, 2, 15);
  }

  bool ManualRotation()
  {
    Vector2 input = new Vector2(
      -Input.GetAxis("Mouse Y"),
      Input.GetAxis("Mouse X")
    );
    const float e = 0.001f;
    if (input.x < e || input.x > e || input.y < e || input.y > e)
    {
      orbitAngles += rotationSpeed * Time.unscaledDeltaTime * input;
      return true;
    }
    return false;
  }

  void UpdateFocusPoint()
  {
    Vector3 targetPoint = Target.position;
    if (focusRadius > 0f)
    {
      float distance = Vector3.Distance(targetPoint, focusPoint);
      float t = 1f;
      if (distance > 0.01f && focusCentering > 0f)
      {
        t = Mathf.Pow(1f - focusCentering, Time.unscaledDeltaTime);
      }
      if (distance > focusRadius)
      {
        t = Mathf.Min(t, focusRadius / distance);
      }
      focusPoint = Vector3.Lerp(targetPoint, focusPoint, t);
    }
    else
    {
      focusPoint = targetPoint;
    }
  }

  void ConstrainAngles()
  {
    orbitAngles.x =
      Mathf.Clamp(orbitAngles.x, minVerticalAngle, maxVerticalAngle);

    if (orbitAngles.y < 0f)
    {
      orbitAngles.y += 360f;
    }
    else if (orbitAngles.y >= 360f)
    {
      orbitAngles.y -= 360f;
    }
  }

  void OnValidate()
  {
    if (maxVerticalAngle < minVerticalAngle)
    {
      maxVerticalAngle = minVerticalAngle;
    }
  }
}