-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerAnimator.cs
More file actions
112 lines (89 loc) · 3.52 KB
/
PlayerAnimator.cs
File metadata and controls
112 lines (89 loc) · 3.52 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class CharacterAnimatorParamId
{
public static readonly int HorizontalSpeed = Animator.StringToHash("HorizontalSpeed");
public static readonly int VerticalSpeed = Animator.StringToHash("VerticalSpeed");
public static readonly int IsGrounded = Animator.StringToHash("IsGrounded");
}
[System.Serializable]
public class CharacterIK
{
public Transform LeftHand;
public Transform RightHand;
public Transform LeftFoot;
public Transform RightFoot;
}
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(PlayerController))]
public class PlayerAnimator : MonoBehaviour
{
public CharacterIK CharacterIK;
private Animator _animator;
private PlayerController _playerController;
private void Awake()
{
_animator = GetComponent<Animator>();
_playerController = GetComponent<PlayerController>();
}
// Update is called once per frame
public void UpdateStates()
{
float normHorizontalSpeed = _playerController.HorizontalVelocity.magnitude / _playerController._movementSettings.MaxHorizontalSpeed;
_animator.SetFloat(CharacterAnimatorParamId.HorizontalSpeed, normHorizontalSpeed);
float jumpSpeed = _playerController._movementSettings.JumpSpeed;
float normVerticalSpeed = _playerController.VerticalVelocity.y.Remap(-jumpSpeed, jumpSpeed, -1.0f, 1.0f);
_animator.SetFloat(CharacterAnimatorParamId.VerticalSpeed, normVerticalSpeed);
_animator.SetBool(CharacterAnimatorParamId.IsGrounded, _playerController.IsGrounded);
}
void OnAnimatorIK()
{
//Right Hand
if(CharacterIK.RightHand != null){
_animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
_animator.SetIKPosition(AvatarIKGoal.RightHand, CharacterIK.RightHand.position);
_animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1);
_animator.SetIKRotation(AvatarIKGoal.RightHand, CharacterIK.RightHand.rotation);
}
//Left Hand
if(CharacterIK.LeftHand != null){
_animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, 1);
_animator.SetIKPosition(AvatarIKGoal.LeftHand, CharacterIK.LeftHand.position);
_animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1);
_animator.SetIKRotation(AvatarIKGoal.LeftHand, CharacterIK.LeftHand.rotation);
}
//Right Foot
if(CharacterIK.RightFoot != null){
_animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1);
_animator.SetIKPosition(AvatarIKGoal.RightFoot, CharacterIK.RightFoot.position);
_animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1);
_animator.SetIKRotation(AvatarIKGoal.RightFoot, CharacterIK.RightFoot.rotation);
}
//Left Foot
if(CharacterIK.LeftFoot != null){
_animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1);
_animator.SetIKPosition(AvatarIKGoal.LeftFoot, CharacterIK.LeftFoot.position);
_animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1);
_animator.SetIKRotation(AvatarIKGoal.LeftFoot, CharacterIK.LeftFoot.rotation);
}
}
/// <summary>
/// Draws Gizmos onto the IK Target transforms
/// </summary>
void OnDrawGizmos()
{
if(CharacterIK.RightHand != null){
Gizmos.DrawWireSphere(CharacterIK.RightHand.position, .1f);
}
if(CharacterIK.LeftHand != null){
Gizmos.DrawWireSphere(CharacterIK.LeftHand.position, .1f);
}
if(CharacterIK.RightFoot != null){
Gizmos.DrawWireSphere(CharacterIK.RightFoot.position, .1f);
}
if(CharacterIK.LeftFoot != null){
Gizmos.DrawWireSphere(CharacterIK.LeftFoot.position, .1f);
}
}
}