diff options
| author | SmallFox <2806143047@qq.com> | 2026-05-21 07:21:06 +0800 |
|---|---|---|
| committer | SmallFox <2806143047@qq.com> | 2026-05-21 07:21:06 +0800 |
| commit | 3dcf88a36c072e42005c48dae419c66996c96f84 (patch) | |
| tree | 76a068834f2f536d83d5f1702fa35de6554da8d0 /Assets/Scripts/CameraHandler.cs | |
| parent | a60a3d9344118915a50f22567bef6bee3f2a2415 (diff) | |
摄像机视角实现
实现跟随自由旋转
Diffstat (limited to 'Assets/Scripts/CameraHandler.cs')
| -rw-r--r-- | Assets/Scripts/CameraHandler.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Assets/Scripts/CameraHandler.cs b/Assets/Scripts/CameraHandler.cs new file mode 100644 index 0000000..c815fae --- /dev/null +++ b/Assets/Scripts/CameraHandler.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Unity.Mathematics; +using UnityEngine; + +namespace DS +{ + public class CameraHandler : MonoBehaviour + { + public Transform cameraTransform; + public Transform targetTransform; + public Transform cameraPivotTransform; + private Transform _myTransform; + private Vector3 _cameraTransformPosition; + private LayerMask _ignoreLayers; + private Vector3 cameraFollowVelocity = Vector3.zero; + + public static CameraHandler singleton; + public float lookSpeed = 0.1f; + public float followSpeed = 0.1f; + public float pivotSpeed = 0.03f; + + private float _targetPosition; + private float _defaultPosition; + private float _lookAngle; + private float _pivotAngle; + public float minimumPivot = -35; + public float maximumPivot = 35; + + public float cameraSphereRadius = 0.2f; + public float cameraCollisionOffSet = 0.2f; + public float minimumCollisionOffSet = 0.2f; + + private void Awake() + { + singleton = this; + _myTransform = transform; + _defaultPosition = cameraTransform.localPosition.z; + _ignoreLayers = ~(1 << 8 | 1 << 9 | 1 << 10); + } + + public void FollowTarget(float delta) + { + Vector3 targetPosition = Vector3.SmoothDamp(_myTransform.position, targetTransform.position, + ref cameraFollowVelocity, delta / followSpeed); + _myTransform.position = targetPosition; + } + + public void HandleCameraRotation(float delta, float mouseInputX, float mouseInputY) + { + _lookAngle += (mouseInputX * lookSpeed) / delta; + _pivotAngle -= (mouseInputY * pivotSpeed) / delta; + _pivotAngle = Mathf.Clamp(_pivotAngle, minimumPivot, maximumPivot); + + Vector3 rotation = Vector3.zero; + rotation.y = _lookAngle; + Quaternion targetRotation = Quaternion.Euler(rotation); + _myTransform.rotation = targetRotation; + + rotation = Vector3.zero; + rotation.x = _pivotAngle; + + targetRotation = Quaternion.Euler(rotation); + cameraPivotTransform.localRotation = targetRotation; + + + } + } +} + |
