-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameCamera.cs
41 lines (34 loc) · 1.1 KB
/
GameCamera.cs
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
using UnityEngine;
public class GameCamera : MonoBehaviour
{
private Transform _cameraTarget;
public float cameraDistance;
private Vector3 _startDirection;
private void Start()
{
_cameraTarget = GameObject.FindGameObjectWithTag(
"Player").transform;
_startDirection = Quaternion.Euler(
transform.eulerAngles) * Vector3.forward;
}
private void LateUpdate()
{
Vector3 desiredCameraPosition;
Quaternion desiredCameraRotation;
CalculateCameraTransform(
out desiredCameraPosition,
out desiredCameraRotation);
var myTransform = transform;
myTransform.position = desiredCameraPosition;
myTransform.rotation = desiredCameraRotation;
}
private void CalculateCameraTransform(
out Vector3 cameraPosition,
out Quaternion cameraRotation)
{
var position = _cameraTarget.position;
cameraPosition = position - _startDirection * cameraDistance;
cameraRotation = Quaternion.LookRotation(
position - cameraPosition);
}
}