diff options
Diffstat (limited to 'Assets/Scripts')
| -rw-r--r-- | Assets/Scripts/AnimatorHandler.cs | 92 | ||||
| -rw-r--r-- | Assets/Scripts/AnimatorHandler.cs.meta | 11 | ||||
| -rw-r--r-- | Assets/Scripts/InputHandler.cs | 54 | ||||
| -rw-r--r-- | Assets/Scripts/InputHandler.cs.meta | 11 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem.meta | 8 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.cs | 252 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.cs.meta | 11 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.inputactions | 109 | ||||
| -rw-r--r-- | Assets/Scripts/InputSystem/PlayerControls.inputactions.meta | 14 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerLocomotion.cs | 91 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerLocomotion.cs.meta | 11 |
11 files changed, 664 insertions, 0 deletions
diff --git a/Assets/Scripts/AnimatorHandler.cs b/Assets/Scripts/AnimatorHandler.cs new file mode 100644 index 0000000..d95d12b --- /dev/null +++ b/Assets/Scripts/AnimatorHandler.cs @@ -0,0 +1,92 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace DS +{ + public class AnimatorHandler : MonoBehaviour + { + public Animator animator; + private int _vertical; + private int _horizontal; + public bool canRotate; + + public void Initialize() + { + animator = GetComponent<Animator>(); + _vertical = Animator.StringToHash("Vertical"); + _horizontal = Animator.StringToHash("Horizontal"); + } + + public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement) + { + #region Vertical + + float v = 0; + + if (verticalMovement > 0 && verticalMovement < 0.55f) + { + v = 0.5f; + } + else if(verticalMovement > 0.55f) + { + v = 1; + } + else if (verticalMovement < 0 && verticalMovement > -0.55f) + { + v = -0.5f; + } + else if(verticalMovement < -0.55f) + { + v = -1; + } + else + { + v = 0; + } + + #endregion + + #region Horizontal + + float h = 0; + + if (horizontalMovement > 0 && horizontalMovement < 0.55f) + { + h = 0.5f; + } + else if(horizontalMovement > 0.55f) + { + h = 1; + } + else if (horizontalMovement < 0 && horizontalMovement > -0.55f) + { + h = -0.5f; + } + else if(horizontalMovement < -0.55f) + { + h = -1; + } + else + { + h = 0; + } + + #endregion + + animator.SetFloat(_vertical,v,0.1f,Time.deltaTime); + animator.SetFloat(_horizontal,h,0.1f,Time.deltaTime); + } + + public void CanRotate() + { + canRotate = true; + } + + public void StopRotation() + { + canRotate = false; + } + } + +} diff --git a/Assets/Scripts/AnimatorHandler.cs.meta b/Assets/Scripts/AnimatorHandler.cs.meta new file mode 100644 index 0000000..ff6ee40 --- /dev/null +++ b/Assets/Scripts/AnimatorHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fe522b80fe58504408be0232c59db82f +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 new file mode 100644 index 0000000..ec72656 --- /dev/null +++ b/Assets/Scripts/InputHandler.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace DS +{ + public class InputHandler : MonoBehaviour + { + public float horizontal; + public float vertical; + public float moveAmount; + public float mouseX; + public float mouseY; + + private PlayerControls _inputActions; + + private Vector2 _movementInput; + private Vector2 _cameraInput; + + public void OnEnable() + { + if (_inputActions == null) + { + _inputActions = new PlayerControls(); + _inputActions.PlayerMovement.Movement.performed += + ctx => _movementInput = ctx.ReadValue<Vector2>(); + _inputActions.PlayerMovement.Camera.performed += ctx => _cameraInput = ctx.ReadValue<Vector2>(); + } + + _inputActions.Enable(); + } + + private void OnDisable() + { + _inputActions.Disable(); + } + + public void TickInput(float delta) + { + MoveInput(delta); + } + + private void MoveInput(float delta) + { + horizontal = _movementInput.x; + vertical = _movementInput.y; + moveAmount = Mathf.Clamp01(Mathf.Abs((horizontal)) + Mathf.Abs(vertical)); + mouseX = _cameraInput.x; + mouseY = _cameraInput.y; + } + } +} + diff --git a/Assets/Scripts/InputHandler.cs.meta b/Assets/Scripts/InputHandler.cs.meta new file mode 100644 index 0000000..39e17cc --- /dev/null +++ b/Assets/Scripts/InputHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c4a9043d6a87ed946b39b4f4cbf2c2b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InputSystem.meta b/Assets/Scripts/InputSystem.meta new file mode 100644 index 0000000..5e2c2e6 --- /dev/null +++ b/Assets/Scripts/InputSystem.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 00f0279abe9c32e45bbad4093e3010f6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InputSystem/PlayerControls.cs b/Assets/Scripts/InputSystem/PlayerControls.cs new file mode 100644 index 0000000..71ef71f --- /dev/null +++ b/Assets/Scripts/InputSystem/PlayerControls.cs @@ -0,0 +1,252 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator +// version 1.7.0 +// from Assets/Scripts/InputSystem/PlayerControls.inputactions +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine.InputSystem; +using UnityEngine.InputSystem.Utilities; + +public partial class @PlayerControls: IInputActionCollection2, IDisposable +{ + public InputActionAsset asset { get; } + public @PlayerControls() + { + asset = InputActionAsset.FromJson(@"{ + ""name"": ""PlayerControls"", + ""maps"": [ + { + ""name"": ""Player Movement"", + ""id"": ""cfb355ad-e0aa-4dad-9111-af6e3f1271b2"", + ""actions"": [ + { + ""name"": ""Movement"", + ""type"": ""PassThrough"", + ""id"": ""1ab3b457-9558-4d1c-b580-1b6a7d3cbd45"", + ""expectedControlType"": ""Vector2"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": false + }, + { + ""name"": ""Camera"", + ""type"": ""Value"", + ""id"": ""bb5b6b4b-5b42-4192-a498-10356a2b125a"", + ""expectedControlType"": ""Vector2"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + } + ], + ""bindings"": [ + { + ""name"": ""WASD"", + ""id"": ""d8a77518-37e0-4dd9-8c87-9a6f159ed71b"", + ""path"": ""2DVector"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""up"", + ""id"": ""4eb43c7b-9a1d-4f13-877b-6b3acf19ad17"", + ""path"": ""<Keyboard>/w"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""down"", + ""id"": ""2228edfc-964a-42e3-b1ba-53aebb8f0d5f"", + ""path"": ""<Keyboard>/s"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""left"", + ""id"": ""a35a6c7a-e429-4cb5-90d4-edf345cd188d"", + ""path"": ""<Keyboard>/a"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""right"", + ""id"": ""c9d8817d-167d-4526-b441-3bfd52363503"", + ""path"": ""<Keyboard>/d"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": """", + ""id"": ""d0d72338-edcb-44b9-a675-823b93f06cc0"", + ""path"": ""<Gamepad>/rightStick"", + ""interactions"": """", + ""processors"": ""StickDeadzone"", + ""groups"": """", + ""action"": ""Camera"", + ""isComposite"": false, + ""isPartOfComposite"": false + }, + { + ""name"": """", + ""id"": ""b1f249ca-76f5-4443-85fe-eff12e361675"", + ""path"": ""<Mouse>/delta"", + ""interactions"": """", + ""processors"": ""NormalizeVector2"", + ""groups"": """", + ""action"": ""Camera"", + ""isComposite"": false, + ""isPartOfComposite"": false + } + ] + } + ], + ""controlSchemes"": [] +}"); + // Player Movement + m_PlayerMovement = asset.FindActionMap("Player Movement", throwIfNotFound: true); + m_PlayerMovement_Movement = m_PlayerMovement.FindAction("Movement", throwIfNotFound: true); + m_PlayerMovement_Camera = m_PlayerMovement.FindAction("Camera", throwIfNotFound: true); + } + + public void Dispose() + { + UnityEngine.Object.Destroy(asset); + } + + public InputBinding? bindingMask + { + get => asset.bindingMask; + set => asset.bindingMask = value; + } + + public ReadOnlyArray<InputDevice>? devices + { + get => asset.devices; + set => asset.devices = value; + } + + public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes; + + public bool Contains(InputAction action) + { + return asset.Contains(action); + } + + public IEnumerator<InputAction> GetEnumerator() + { + return asset.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Enable() + { + asset.Enable(); + } + + public void Disable() + { + asset.Disable(); + } + + public IEnumerable<InputBinding> bindings => asset.bindings; + + public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false) + { + return asset.FindAction(actionNameOrId, throwIfNotFound); + } + + public int FindBinding(InputBinding bindingMask, out InputAction action) + { + return asset.FindBinding(bindingMask, out action); + } + + // Player Movement + private readonly InputActionMap m_PlayerMovement; + private List<IPlayerMovementActions> m_PlayerMovementActionsCallbackInterfaces = new List<IPlayerMovementActions>(); + private readonly InputAction m_PlayerMovement_Movement; + private readonly InputAction m_PlayerMovement_Camera; + public struct PlayerMovementActions + { + private @PlayerControls m_Wrapper; + public PlayerMovementActions(@PlayerControls wrapper) { m_Wrapper = wrapper; } + public InputAction @Movement => m_Wrapper.m_PlayerMovement_Movement; + public InputAction @Camera => m_Wrapper.m_PlayerMovement_Camera; + public InputActionMap Get() { return m_Wrapper.m_PlayerMovement; } + public void Enable() { Get().Enable(); } + public void Disable() { Get().Disable(); } + public bool enabled => Get().enabled; + public static implicit operator InputActionMap(PlayerMovementActions set) { return set.Get(); } + public void AddCallbacks(IPlayerMovementActions instance) + { + if (instance == null || m_Wrapper.m_PlayerMovementActionsCallbackInterfaces.Contains(instance)) return; + m_Wrapper.m_PlayerMovementActionsCallbackInterfaces.Add(instance); + @Movement.started += instance.OnMovement; + @Movement.performed += instance.OnMovement; + @Movement.canceled += instance.OnMovement; + @Camera.started += instance.OnCamera; + @Camera.performed += instance.OnCamera; + @Camera.canceled += instance.OnCamera; + } + + private void UnregisterCallbacks(IPlayerMovementActions instance) + { + @Movement.started -= instance.OnMovement; + @Movement.performed -= instance.OnMovement; + @Movement.canceled -= instance.OnMovement; + @Camera.started -= instance.OnCamera; + @Camera.performed -= instance.OnCamera; + @Camera.canceled -= instance.OnCamera; + } + + public void RemoveCallbacks(IPlayerMovementActions instance) + { + if (m_Wrapper.m_PlayerMovementActionsCallbackInterfaces.Remove(instance)) + UnregisterCallbacks(instance); + } + + public void SetCallbacks(IPlayerMovementActions instance) + { + foreach (var item in m_Wrapper.m_PlayerMovementActionsCallbackInterfaces) + UnregisterCallbacks(item); + m_Wrapper.m_PlayerMovementActionsCallbackInterfaces.Clear(); + AddCallbacks(instance); + } + } + public PlayerMovementActions @PlayerMovement => new PlayerMovementActions(this); + public interface IPlayerMovementActions + { + void OnMovement(InputAction.CallbackContext context); + void OnCamera(InputAction.CallbackContext context); + } +} diff --git a/Assets/Scripts/InputSystem/PlayerControls.cs.meta b/Assets/Scripts/InputSystem/PlayerControls.cs.meta new file mode 100644 index 0000000..7bccbc7 --- /dev/null +++ b/Assets/Scripts/InputSystem/PlayerControls.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8f290c6d6ff027e439a0eabfe764b011 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InputSystem/PlayerControls.inputactions b/Assets/Scripts/InputSystem/PlayerControls.inputactions new file mode 100644 index 0000000..ed31d4a --- /dev/null +++ b/Assets/Scripts/InputSystem/PlayerControls.inputactions @@ -0,0 +1,109 @@ +{ + "name": "PlayerControls", + "maps": [ + { + "name": "Player Movement", + "id": "cfb355ad-e0aa-4dad-9111-af6e3f1271b2", + "actions": [ + { + "name": "Movement", + "type": "PassThrough", + "id": "1ab3b457-9558-4d1c-b580-1b6a7d3cbd45", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Camera", + "type": "Value", + "id": "bb5b6b4b-5b42-4192-a498-10356a2b125a", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + } + ], + "bindings": [ + { + "name": "WASD", + "id": "d8a77518-37e0-4dd9-8c87-9a6f159ed71b", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "4eb43c7b-9a1d-4f13-877b-6b3acf19ad17", + "path": "<Keyboard>/w", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "2228edfc-964a-42e3-b1ba-53aebb8f0d5f", + "path": "<Keyboard>/s", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "a35a6c7a-e429-4cb5-90d4-edf345cd188d", + "path": "<Keyboard>/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "c9d8817d-167d-4526-b441-3bfd52363503", + "path": "<Keyboard>/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "d0d72338-edcb-44b9-a675-823b93f06cc0", + "path": "<Gamepad>/rightStick", + "interactions": "", + "processors": "StickDeadzone", + "groups": "", + "action": "Camera", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "b1f249ca-76f5-4443-85fe-eff12e361675", + "path": "<Mouse>/delta", + "interactions": "", + "processors": "NormalizeVector2", + "groups": "", + "action": "Camera", + "isComposite": false, + "isPartOfComposite": false + } + ] + } + ], + "controlSchemes": [] +}
\ No newline at end of file diff --git a/Assets/Scripts/InputSystem/PlayerControls.inputactions.meta b/Assets/Scripts/InputSystem/PlayerControls.inputactions.meta new file mode 100644 index 0000000..d54b310 --- /dev/null +++ b/Assets/Scripts/InputSystem/PlayerControls.inputactions.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: 951b38ef40b38c44cb85c66fdd40eccc +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} + generateWrapperCode: 1 + wrapperCodePath: + wrapperClassName: + wrapperCodeNamespace: diff --git a/Assets/Scripts/PlayerLocomotion.cs b/Assets/Scripts/PlayerLocomotion.cs new file mode 100644 index 0000000..686595d --- /dev/null +++ b/Assets/Scripts/PlayerLocomotion.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Unity.Mathematics; +using UnityEngine; + + +namespace DS +{ + public class PlayerLocomotion : MonoBehaviour + { + private Transform _cameraObject; //存储Camera的位置 + private InputHandler _inputHandler; + private Vector3 _moveDirection; + + [HideInInspector] public Transform myTransform; + [HideInInspector] public AnimatorHandler animatorHandler; + + public new Rigidbody rigidbody; + public GameObject normalCamera; //引用Camera + + + [Header("Stats")] + [SerializeField] private float movementSpeed = 5; + [SerializeField] private float rotationSpeed = 10; + private void Start() + { + rigidbody = GetComponent<Rigidbody>(); + _inputHandler = GetComponent<InputHandler>(); + animatorHandler = GetComponentInChildren<AnimatorHandler>(); + _cameraObject = Camera.main.transform; + myTransform = transform; + animatorHandler.Initialize(); + } + + private void Update() + { + float delta = Time.deltaTime; + _inputHandler.TickInput(delta); + + _moveDirection = _cameraObject.forward * _inputHandler.vertical; + _moveDirection += _cameraObject.right * _inputHandler.horizontal; + _moveDirection.Normalize(); + + float speed = movementSpeed; + _moveDirection *= speed; + + Vector3 projectedVelocity = Vector3.ProjectOnPlane(_moveDirection, _normalVector); + rigidbody.velocity = projectedVelocity; + + animatorHandler.UpdateAnimatorValues(_inputHandler.moveAmount,0); + + if (animatorHandler.canRotate) + { + HandleRotation(delta); + } + } + + #region Movement + + private Vector3 _normalVector; + private Vector3 _targetPosition; + + private void HandleRotation(float delta) + { + Vector3 targetDir = Vector3.zero; + float moveOverride = _inputHandler.moveAmount; + + targetDir = _cameraObject.forward * _inputHandler.vertical; + targetDir += _cameraObject.right * _inputHandler.horizontal; + + targetDir.Normalize(); + targetDir.y = 0; + + if (targetDir == Vector3.zero) + { + targetDir = myTransform.forward; + } + + float rs = rotationSpeed; + Quaternion tr = Quaternion.LookRotation(targetDir); + Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta); + + myTransform.rotation = targetRotation; + } + + + #endregion + } +} + diff --git a/Assets/Scripts/PlayerLocomotion.cs.meta b/Assets/Scripts/PlayerLocomotion.cs.meta new file mode 100644 index 0000000..9efd5c5 --- /dev/null +++ b/Assets/Scripts/PlayerLocomotion.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4ca3b9320dac3304ba425bd0d3952131 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |
