first commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.Characters
|
||||
{
|
||||
public sealed class ThirdPersonCamera : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform target;
|
||||
[SerializeField] private float mouseSensitivity = 2.5f;
|
||||
[SerializeField] private float smoothTime = 0.12f;
|
||||
[SerializeField] private float minPitch = 45f;
|
||||
[SerializeField] private float maxPitch = 60f;
|
||||
[SerializeField] private float initialPitch = 52f;
|
||||
[SerializeField] private float distance = 14f;
|
||||
[SerializeField] private float lookAhead = 3.5f;
|
||||
[SerializeField] private float targetHeight = 1.25f;
|
||||
|
||||
private Vector3 velocity;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
|
||||
public void SetTarget(Transform newTarget)
|
||||
{
|
||||
target = newTarget;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
yaw = transform.eulerAngles.y;
|
||||
pitch = Mathf.Clamp(initialPitch, minPitch, maxPitch);
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (target == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
else if (Input.GetMouseButtonUp(1))
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
|
||||
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
|
||||
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
|
||||
}
|
||||
|
||||
Vector3 forward = Quaternion.Euler(0f, yaw, 0f) * Vector3.forward;
|
||||
float pitchRadians = pitch * Mathf.Deg2Rad;
|
||||
float horizontalDistance = Mathf.Cos(pitchRadians) * distance;
|
||||
float height = Mathf.Sin(pitchRadians) * distance;
|
||||
|
||||
Vector3 desiredPosition = target.position - forward * horizontalDistance + Vector3.up * height;
|
||||
Vector3 lookTarget = target.position + forward * lookAhead + Vector3.up * targetHeight;
|
||||
|
||||
transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothTime);
|
||||
transform.rotation = Quaternion.LookRotation(lookTarget - transform.position, Vector3.up);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user