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 | |
| parent | a60a3d9344118915a50f22567bef6bee3f2a2415 (diff) | |
摄像机视角实现
实现跟随自由旋转
Diffstat (limited to 'Assets/Scripts')
| -rw-r--r-- | Assets/Scripts/CameraHandler.cs | 71 | ||||
| -rw-r--r-- | Assets/Scripts/CameraHandler.cs.meta | 11 | ||||
| -rw-r--r-- | Assets/Scripts/InputHandler.cs | 18 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.cs | 2 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.inputactions | 2 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerLocomotion.cs | 1 |
6 files changed, 103 insertions, 2 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; + + + } + } +} + diff --git a/Assets/Scripts/CameraHandler.cs.meta b/Assets/Scripts/CameraHandler.cs.meta new file mode 100644 index 0000000..a828ef6 --- /dev/null +++ b/Assets/Scripts/CameraHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eb1b68ec627ba6d45a0ea7b392c986b2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InputHandler.cs b/Assets/Scripts/InputHandler.cs index ec72656..f653adb 100644 --- a/Assets/Scripts/InputHandler.cs +++ b/Assets/Scripts/InputHandler.cs @@ -14,10 +14,27 @@ namespace DS public float mouseY; private PlayerControls _inputActions; + private CameraHandler _cameraHandler; private Vector2 _movementInput; private Vector2 _cameraInput; + private void Awake() + { + _cameraHandler = CameraHandler.singleton; + } + + private void FixedUpdate() + { + float delta = Time.fixedDeltaTime; + + if (_cameraHandler != null) + { + _cameraHandler.FollowTarget(delta); + _cameraHandler.HandleCameraRotation(delta,mouseX,mouseY); + } + } + public void OnEnable() { if (_inputActions == null) @@ -48,6 +65,7 @@ namespace DS moveAmount = Mathf.Clamp01(Mathf.Abs((horizontal)) + Mathf.Abs(vertical)); mouseX = _cameraInput.x; mouseY = _cameraInput.y; + } } } diff --git a/Assets/Scripts/InputSystem/PlayerControls.cs b/Assets/Scripts/InputSystem/PlayerControls.cs index 71ef71f..c76f6ba 100644 --- a/Assets/Scripts/InputSystem/PlayerControls.cs +++ b/Assets/Scripts/InputSystem/PlayerControls.cs @@ -38,7 +38,7 @@ public partial class @PlayerControls: IInputActionCollection2, IDisposable }, { ""name"": ""Camera"", - ""type"": ""Value"", + ""type"": ""PassThrough"", ""id"": ""bb5b6b4b-5b42-4192-a498-10356a2b125a"", ""expectedControlType"": ""Vector2"", ""processors"": """", diff --git a/Assets/Scripts/InputSystem/PlayerControls.inputactions b/Assets/Scripts/InputSystem/PlayerControls.inputactions index ed31d4a..e71a5c9 100644 --- a/Assets/Scripts/InputSystem/PlayerControls.inputactions +++ b/Assets/Scripts/InputSystem/PlayerControls.inputactions @@ -16,7 +16,7 @@ }, { "name": "Camera", - "type": "Value", + "type": "PassThrough", "id": "bb5b6b4b-5b42-4192-a498-10356a2b125a", "expectedControlType": "Vector2", "processors": "", diff --git a/Assets/Scripts/PlayerLocomotion.cs b/Assets/Scripts/PlayerLocomotion.cs index 686595d..aaa0c12 100644 --- a/Assets/Scripts/PlayerLocomotion.cs +++ b/Assets/Scripts/PlayerLocomotion.cs @@ -41,6 +41,7 @@ namespace DS _moveDirection = _cameraObject.forward * _inputHandler.vertical; _moveDirection += _cameraObject.right * _inputHandler.horizontal; _moveDirection.Normalize(); + _moveDirection.y = 0; float speed = movementSpeed; _moveDirection *= speed; |
