aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/PlayerLocomotion.cs
blob: 9f058b036e9d8c814da8c36d88d29be45055b245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;


namespace DS
{
    public class PlayerLocomotion : MonoBehaviour
    {
        // 是否使用根动画
        [Range(0, 1)] public float rootMotionBlending = 1f;

        public bool usingRootMotion
        {
            get => rootMotionBlending > 0.5f;
            set => rootMotionBlending = value ? 0 : 1;
        }
        
        [HideInInspector] public Vector3 rootMotion; 
        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()
        {
            UpdateCharacterMovement();
        }

        private void FixedUpdate()
        {
            
        }

        private void UpdateCharacterMovement()
        {
            float delta = Time.deltaTime;
            _inputHandler.TickInput(delta);

            _moveDirection = _cameraObject.forward * _inputHandler.vertical;
            _moveDirection += _cameraObject.right * _inputHandler.horizontal;
            _moveDirection.Normalize();
            _moveDirection.y = 0;

            float speed = movementSpeed;
            _moveDirection *= speed;
            
            var baseVelocity = Vector3.Lerp(rootMotion, _moveDirection, rootMotionBlending);
            Vector3 projectedVelocity = Vector3.ProjectOnPlane(baseVelocity, _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
    }
}