-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.cs
160 lines (132 loc) · 5.32 KB
/
Character.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using UnityEngine;
using UnityEngine.AI;
using RPG.CameraUI; //TODO considerar reescrever
namespace RPG.Personagem
{
[SelectionBase]
public class Character : MonoBehaviour
{
[Header("Capsule Collider")]
[SerializeField] Vector3 colliderCenter = new Vector3(0, 0.8f, 0);
[SerializeField] float colliderRadius = 0.3f;
[SerializeField] float colliderHeight = 1.6f;
[Header("Animator")]
[SerializeField] RuntimeAnimatorController animatorController;
[SerializeField] AnimatorOverrideController animatorOverideController;
[SerializeField] Avatar characterAvatar;
[Header("Movement")]
[SerializeField] float moveSpeedMultiplier = .7f;
[SerializeField] float animatioSpeedMutiplier = 1.5f;
[SerializeField] float movingTurnSpeed = 360;
[SerializeField] float stationaryTurnSpeed = 180;
[SerializeField] float moveThreshold = 1f;
[SerializeField] [Range(.1f, 1f)] float animatorForwardCap = 1f;
[Header("Audio")]
[SerializeField] float audioSpetial = 1f;
[SerializeField] float audioVolume = 1f;
[Header("NavMeshAgent")]
[SerializeField] float navMeshSteeringSpeed = 1f;
[SerializeField] float navMeshStoppingDistance = 1.5f;
float turnAmount;
float forwardAmount;
NavMeshAgent navMeshAgent;
Animator myAnimator;
Rigidbody myRigidBody;
bool isAlive = true;
bool isInDirectMode = false;
void Awake()
{
AddRequiredComponents();
}
private void AddRequiredComponents()
{
var capsuleCollider = gameObject.AddComponent<CapsuleCollider>();
capsuleCollider.center = colliderCenter;
capsuleCollider.height = colliderHeight;
capsuleCollider.radius = colliderRadius;
myRigidBody = gameObject.AddComponent<Rigidbody>();
myRigidBody.constraints = RigidbodyConstraints.FreezeRotation;
myAnimator = gameObject.AddComponent<Animator>();
myAnimator.runtimeAnimatorController = animatorController;
myAnimator.avatar = characterAvatar;
var audioSource = gameObject.AddComponent<AudioSource>();
audioSource.spatialBlend = audioSpetial;
audioSource.volume = audioVolume;
navMeshAgent = gameObject.AddComponent<NavMeshAgent>();
navMeshAgent.stoppingDistance = navMeshStoppingDistance;
navMeshAgent.autoBraking = false;
navMeshAgent.updatePosition = true;
navMeshAgent.updateRotation = false;
navMeshAgent.speed = navMeshSteeringSpeed;
}
void Update()
{
if (navMeshAgent.remainingDistance > navMeshAgent.stoppingDistance && isAlive)
{
Move(navMeshAgent.desiredVelocity);
}
else
{
Move(Vector3.zero);
}
}
public float GetAnimSpeedMultiplier()
{
return myAnimator.speed;
}
public void SetDestination(Vector3 worldPos)
{
navMeshAgent.destination = worldPos;
}
void Move(Vector3 movement)
{
SetForwardAndTurn(movement);
ApplyExtraTurnRotation();
UpdateAnimator();
}
void SetForwardAndTurn(Vector3 movement)
{
if (movement.magnitude > moveThreshold)
{
movement.Normalize();
}
var localMove = transform.InverseTransformDirection(movement);
turnAmount = Mathf.Atan2(localMove.x, localMove.z);
forwardAmount = localMove.z;
}
public AnimatorOverrideController GetOverrideController()
{
return animatorOverideController;
}
void UpdateAnimator()
{
// update the animator parameters
myAnimator.SetFloat("Forward", forwardAmount * animatorForwardCap, 0.1f, Time.deltaTime);
myAnimator.SetFloat("Turn", turnAmount, 0.1f, Time.deltaTime);
myAnimator.speed = animatioSpeedMutiplier;
}
void ApplyExtraTurnRotation()
{
// help the character turn faster (this is in addition to root rotation in the animation)
float turnSpeed = Mathf.Lerp(stationaryTurnSpeed, movingTurnSpeed, forwardAmount);
transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime, 0);
}
void OnAnimatorMove()
{
// we implement this function to override the default root motion.
// this allows us to modify the positional speed before it's applied.
if (Time.deltaTime > 0)
{
Vector3 velocity = (myAnimator.deltaPosition * moveSpeedMultiplier) / Time.deltaTime;
// we preserve the existing y part of the current velocity.
velocity.y = myRigidBody.velocity.y;
myRigidBody.velocity = velocity;
}
}
public void Kill()
{
isAlive = false;
}
}
}