aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/AnimatorHandler.cs
blob: b71c573e6ca93f117c7fd146306acc7b57460fdd (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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DS
{
    public class AnimatorHandler : MonoBehaviour
    {
        public PlayerLocomotion playerLocomotion;
        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;
        }

        private void OnAnimatorMove()
        {
            Debug.Log(animator.velocity);
            playerLocomotion.rootMotion = animator.velocity;
        }

        private void OnDrawGizmos()
        {
            // 我们先假定 地板永远是朝上的
            var normalVector = Vector3.up;
            Vector3 rootMotionProjected = Vector3.ProjectOnPlane(animator.velocity, normalVector);
            Gizmos.DrawLine(transform.position, transform.position + rootMotionProjected * 2f);
        }
    }

}