| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class FreeMovementController : MonoBehaviour |
| | 7 | | { |
| | 8 | |
|
| | 9 | | [Header("Movement")] |
| 598 | 10 | | public float groundCheckExtraDistance = 0.25f; |
| 598 | 11 | | public float movementSpeed = 11f; |
| 598 | 12 | | public float runningSpeedMultiplier = 0.36f; |
| | 13 | |
|
| | 14 | | [Header("InputActions")] |
| | 15 | | public InputAction_Hold sprintAction; |
| | 16 | |
|
| | 17 | | [System.NonSerialized] public CharacterController characterController; |
| | 18 | |
|
| | 19 | | bool isSprinting = false; |
| | 20 | | bool isActive; |
| | 21 | |
|
| 598 | 22 | | Vector3 velocity = Vector3.zero; |
| | 23 | |
|
| 0 | 24 | | private Vector3NullableVariable characterForward => CommonScriptableObjects.characterForward; |
| | 25 | |
|
| 0 | 26 | | private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward; |
| 0 | 27 | | private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight; |
| | 28 | |
|
| | 29 | | [SerializeField] private InputAction_Measurable characterYAxis; |
| | 30 | | [SerializeField] private InputAction_Measurable characterXAxis; |
| | 31 | |
|
| 1194 | 32 | | private void Awake() { characterController = GetComponent<CharacterController>(); } |
| | 33 | |
|
| 0 | 34 | | public bool IsActive() { return isActive; } |
| | 35 | |
|
| 0 | 36 | | public void SetActive(bool _isActive) { isActive = _isActive; } |
| | 37 | |
|
| 0 | 38 | | public Vector3 CalculateMovement() { return FreeMovement(); } |
| | 39 | |
|
| | 40 | | Vector3 FreeMovement() |
| | 41 | | { |
| 0 | 42 | | velocity.x = 0f; |
| 0 | 43 | | velocity.z = 0f; |
| 0 | 44 | | velocity.y = 0; |
| | 45 | |
|
| 0 | 46 | | if (characterController.enabled) |
| | 47 | | { |
| 0 | 48 | | var speed = movementSpeed * (isSprinting ? runningSpeedMultiplier : 1f); |
| | 49 | |
|
| 0 | 50 | | transform.forward = characterForward.Get().Value; |
| | 51 | |
|
| 0 | 52 | | var xzPlaneForward = Vector3.Scale(cameraForward.Get(), new Vector3(1, 0, 1)); |
| 0 | 53 | | var xzPlaneRight = Vector3.Scale(cameraRight.Get(), new Vector3(1, 0, 1)); |
| | 54 | |
|
| 0 | 55 | | Vector3 forwardTarget = Vector3.zero; |
| | 56 | |
|
| 0 | 57 | | if (characterYAxis.GetValue() > 0) |
| 0 | 58 | | forwardTarget += xzPlaneForward; |
| 0 | 59 | | if (characterYAxis.GetValue() < 0) |
| 0 | 60 | | forwardTarget -= xzPlaneForward; |
| | 61 | |
|
| 0 | 62 | | if (characterXAxis.GetValue() > 0) |
| 0 | 63 | | forwardTarget += xzPlaneRight; |
| 0 | 64 | | if (characterXAxis.GetValue() < 0) |
| 0 | 65 | | forwardTarget -= xzPlaneRight; |
| | 66 | |
|
| | 67 | |
|
| 0 | 68 | | if (Input.GetKey(KeyCode.Space)) |
| 0 | 69 | | forwardTarget += Vector3.up; |
| 0 | 70 | | else if (Input.GetKey(KeyCode.X)) |
| 0 | 71 | | forwardTarget += Vector3.down; |
| | 72 | |
|
| 0 | 73 | | forwardTarget.Normalize(); |
| | 74 | |
|
| | 75 | |
|
| 0 | 76 | | velocity += forwardTarget * speed; |
| 0 | 77 | | CommonScriptableObjects.playerUnityEulerAngles.Set(transform.eulerAngles); |
| | 78 | |
|
| 0 | 79 | | characterController.Move(velocity * Time.deltaTime); |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | return velocity; |
| | 83 | | } |
| | 84 | | } |