61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using System;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace CameraScripts
|
|
{
|
|
public class CameraHandler : MonoBehaviour
|
|
{
|
|
public Transform targetTransform;
|
|
public Transform cameraTransform;
|
|
public Transform cameraPivotTransform;
|
|
private Transform _myTransform;
|
|
private Vector3 _cameraTransformPosition;
|
|
private LayerMask ignoreLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
|
|
|
|
public static CameraHandler Singleton;
|
|
|
|
public const float LookSpeed = 0.1f;
|
|
public const float FollowSpeed = 0.1f;
|
|
public const float PivotSpeed = 0.03f;
|
|
|
|
private float _defaultPosition;
|
|
private float _lookAngle;
|
|
private float _pivotAngle;
|
|
|
|
public float minimumPivot = -35;
|
|
public float maximumPivot = 35;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
Application.targetFrameRate = 60;
|
|
Singleton = this;
|
|
_myTransform = transform;
|
|
_defaultPosition = _myTransform.localPosition.z;
|
|
}
|
|
|
|
public void TargetPosition(float delta)
|
|
{
|
|
var toTargetPosition = Vector3.Lerp(_myTransform.position, targetTransform.position, delta /FollowSpeed);
|
|
_myTransform.position = toTargetPosition;
|
|
}
|
|
|
|
public void HandleCameraRotation(float delta, float mouseX, float mouseY)
|
|
{
|
|
_lookAngle += (mouseX * LookSpeed) / delta;
|
|
_pivotAngle -= (mouseY * PivotSpeed) / delta;
|
|
_pivotAngle = Mathf.Clamp(_pivotAngle, minimumPivot, maximumPivot);
|
|
|
|
var rotation = Vector3.zero;
|
|
rotation.y = _lookAngle;
|
|
var targetRotation = Quaternion.Euler(rotation);
|
|
_myTransform.rotation = targetRotation;
|
|
|
|
rotation = Vector3.zero;
|
|
rotation.x = _pivotAngle;
|
|
targetRotation = Quaternion.Euler(rotation);
|
|
cameraPivotTransform.localRotation = targetRotation;
|
|
}
|
|
}
|
|
} |