Skip to content

Commit 4793148

Browse files
committed
Humanoid animator manager
Fixed humanoid so max speed has a hardset limit of 1.6x greater than the set max speed in the event of running down very steep inclines. This is to keep the Animator smooth. The new HumanoidAnim also assumes that the user has FinalIK installed. This can easily be removed though via the deletion of the component reference.
1 parent f1161d7 commit 4793148

7 files changed

+80
-36
lines changed

Humanoid.cs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ public class Humanoid : MonoBehaviour
66
{
77
protected GroundCheck gndCheck; // If grounded, incline checks, etc.
88
protected CharacterController controller; // Movement handling.
9-
protected float Gravity { get; set; } = .04f;
9+
protected float Gravity { get; set; } = .032f;
1010

11-
private bool startedJmpCd = false; // Only allow one active jump cooldown.
12-
private bool canJump = true; // Jump cooldown lock.
13-
14-
private float curSpd, maxSpd;
15-
16-
public float jogSpd = 2, runSpd = 4;
17-
public float jumpAmount = 2, jumpCooldown = .4f;
11+
[HideInInspector] public bool StartedJmpCd { get; private set; } = false; // Only allow one active jump cooldown.
12+
[HideInInspector] public bool CanJump { get; private set; } = true; // Jump cooldown lock.
13+
[HideInInspector] public float CurSpd { get; private set; }
14+
[HideInInspector] public float MaxSpd { get; private set; }
1815

16+
public float jogSpd = 1.45f, runSpd = 2f;
17+
public float jumpAmount = 0.35f, jumpCooldown = 0.8f;
18+
public float accelMod = 10; // Used to calculate cur speed.
1919

2020
void Awake()
2121
{
@@ -27,23 +27,23 @@ void Awake()
2727
// Update is called once per frame
2828
protected void Update()
2929
{
30-
if (gndCheck.Grounded && !canJump) // Start jump cooldown once grounded.
30+
SetCurSpeed(MaxSpd > 0, CurSpd, MaxSpd);
31+
GravityCalc(Gravity);
32+
33+
if (gndCheck.Grounded && !CanJump) // Start jump cooldown once grounded.
3134
StartCoroutine(JumpCooldown(jumpCooldown));
3235

33-
SetCurSpeed(maxSpd > 0, curSpd, maxSpd);
34-
GravityCalc(Gravity);
3536
ApplyMoveVelocity();
3637

3738
if (gndCheck.Grounded) // If grounded, limit move speed.
3839
{
3940
moveVel.x = 0;
4041
moveVel.z = 0;
4142
}
42-
else canJump = false; // Can't jump if not grounded.
43-
Debug.Log(controller.velocity.magnitude);
43+
else CanJump = false; // Can't jump if not grounded.
4444
}
4545

46-
46+
[SerializeField] private float rotSpdPenalty = .4f;
4747
/// <summary>
4848
/// Rotates humanoid to look at target position.
4949
/// </summary>
@@ -52,10 +52,13 @@ protected void Update()
5252
/// <param name="rotSpeed"></param>
5353
protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
5454
{
55-
Vector3 curMoveDir = Vector3.Lerp(transform.position, newDir, dampening);
55+
// Greater speed decreases turn speed.
56+
float spdRotPenalty = CurSpd * rotSpdPenalty; // Used on dampening & rotation speed.
57+
58+
Vector3 curMoveDir = Vector3.Lerp(transform.position, newDir, (dampening + spdRotPenalty));
5659
// Lerp from cur rot to new one.
5760
transform.rotation = (Quaternion.Slerp(transform.rotation,
58-
Quaternion.LookRotation(new Vector3(curMoveDir.x, 0, curMoveDir.z)), rotSpeed * Time.deltaTime));
61+
Quaternion.LookRotation(new Vector3(curMoveDir.x, 0, curMoveDir.z)), (rotSpeed - spdRotPenalty) * Time.deltaTime));
5962
}
6063

6164

@@ -73,19 +76,23 @@ protected void Move(float speed)
7376
{
7477
// Max speed more/less depending on terrain incline.
7578
if (controller.velocity.y >= 0)
76-
maxSpd = (speed + CurVel) - GndSpdMod; // Less top speed if go up incline.
77-
else maxSpd = (speed + CurVel) + GndSpdMod; // More top speed if down incline.
79+
MaxSpd = (speed + CurVel) - GndSpdMod; // Less top speed if go up incline.
80+
else MaxSpd = (speed + CurVel) + GndSpdMod; // More top speed if down incline.
7881

79-
// Limit min/top max speed.
80-
if (maxSpd < 0) maxSpd = 0;
81-
else if (maxSpd > speed * 2) maxSpd = speed * 2;
82+
// Limit min/top max speed.
83+
if(speed != 0)
84+
{
85+
if (MaxSpd < 0) MaxSpd = 0;
86+
else if (MaxSpd > speed * 1.6f)
87+
MaxSpd = speed * 1.6f;
88+
}
89+
8290

8391
if (gndCheck.Grounded)
84-
moveVel += (transform.forward * curSpd) * Time.deltaTime;
92+
moveVel += (transform.forward * CurSpd) * Time.deltaTime;
8593
}
8694

8795

88-
public float accelMod = 1;
8996
/// <summary>
9097
/// Accel/Decel for current speed.
9198
/// </summary>
@@ -96,7 +103,7 @@ protected void Move(float speed)
96103
private void SetCurSpeed(bool moving, float curSpeed, float maxBaseSpd)
97104
{
98105
float acclBonus = 0;
99-
if (maxBaseSpd <= maxSpd) // Down slope - accel faster.
106+
if (maxBaseSpd <= MaxSpd) // Down slope - accel faster.
100107
acclBonus += (CurVel + GndSpdMod) / 3;
101108
else acclBonus -= (CurVel + GndSpdMod) / 3;
102109

@@ -105,11 +112,11 @@ private void SetCurSpeed(bool moving, float curSpeed, float maxBaseSpd)
105112
else curSpeed -= (accelMod + acclBonus) * Time.deltaTime;
106113

107114
// Limit min/max speed.
108-
if (curSpeed > maxSpd)
109-
curSpeed = maxSpd;
110-
else if (curSpd < 0)
115+
if (curSpeed > MaxSpd)
116+
curSpeed = MaxSpd;
117+
else if (curSpeed < 0)
111118
curSpeed = 0;
112-
curSpd = curSpeed;
119+
CurSpd = curSpeed;
113120
}
114121

115122

@@ -130,15 +137,15 @@ private void GravityCalc(float gravity)
130137
// Debug.Log(gndCheck.Grounded);
131138
}
132139

133-
140+
134141
/// <summary>
135-
/// If not on cooldown, add jump force to vertical move velocity.
142+
/// If not on cooldown, add jump force to vertical move velocity & give small dist boost.
136143
/// </summary>
137144
/// <param name="amount"></param>
138145
/// <param name="cooldown"></param>
139146
protected void Jump()
140147
{
141-
if (!canJump || !gndCheck.Grounded) return;
148+
if (!CanJump || !gndCheck.Grounded) return;
142149
moveVel.y += jumpAmount;
143150
}
144151

@@ -150,12 +157,12 @@ protected void Jump()
150157
/// <returns></returns>
151158
private IEnumerator JumpCooldown(float cooldown)
152159
{
153-
if (!startedJmpCd) // Cooldown once.
160+
if (!StartedJmpCd) // Cooldown once.
154161
{
155-
startedJmpCd = true;
162+
StartedJmpCd = true;
156163
yield return new WaitForSeconds(cooldown);
157-
canJump = true;
158-
startedJmpCd = false;
164+
CanJump = true;
165+
StartedJmpCd = false;
159166
}
160167
}
161168

@@ -165,7 +172,7 @@ private IEnumerator JumpCooldown(float cooldown)
165172
/// </summary>
166173
private void ApplyMoveVelocity()
167174
{
168-
if(maxSpd == 0)
175+
if(MaxSpd == 0)
169176
{
170177
moveVel.x = 0;
171178
moveVel.z = 0;

HumanoidAnim.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using RootMotion.FinalIK;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class HumanoidAnim : MonoBehaviour
7+
{
8+
private GrounderFBBIK grounderIK;
9+
private GroundCheck gndCheck;
10+
private Humanoid humanoid;
11+
private Animator anim;
12+
13+
// Start is called before the first frame update
14+
void Start()
15+
{
16+
grounderIK = GetComponent<GrounderFBBIK>();
17+
humanoid = GetComponent<Humanoid>();
18+
gndCheck = GetComponent<GroundCheck>();
19+
anim = GetComponent<Animator>();
20+
}
21+
22+
// Update is called once per frame
23+
void Update()
24+
{
25+
// Jumping if can't jump and not on cooldown after landing.
26+
bool jumping = !humanoid.CanJump && !humanoid.StartedJmpCd;
27+
28+
if (jumping) // Allow feet to leave ground when jump.
29+
grounderIK.weight -= (float)(2.5 * Time.deltaTime);
30+
else
31+
grounderIK.weight += (float) (2.5 * Time.deltaTime);
32+
33+
anim.SetBool("Jump", jumping);
34+
anim.SetFloat("Speed", humanoid.CurSpd);
35+
anim.SetBool("OnGround", gndCheck.Grounded);
36+
}
37+
}

Player inspector components.PNG

-24.7 KB
Binary file not shown.
53.5 KB
Loading
45.5 KB
Loading
130 KB
Loading

0 commit comments

Comments
 (0)