Skip to content

Commit 1fc85b6

Browse files
committed
Abstracted general movement into Component.
Moved Humanoid's Move() functionality to a 'Humanoid Move Standard' component. This is to enable a combat module with a different movement system and to further Abstract/Encapsulate the humanoid's complexity from the end-user child classes. Thereby making it easier to both manage the code and build humanoid AI controllers.
1 parent e2152d6 commit 1fc85b6

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

Humanoid.cs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class Humanoid : MonoBehaviour
66
{
77
protected GroundCheck gndCheck; // If grounded, incline checks, etc.
88
protected CharacterController controller; // Movement handling.
9+
private HumanoidMoveStd stdMovement; // Handles generall non-combat movement.
10+
911
public float Gravity = .032f;// { get; set; } = .032f;
1012

1113
[HideInInspector] public bool StartedJmpCd { get; private set; } = false; // Only allow one active jump cooldown.
@@ -17,19 +19,21 @@ public class Humanoid : MonoBehaviour
1719
public float jumpAmount = 0.35f, jumpCooldown = 0.8f;
1820
public float accelMod = 10; // Used to calculate cur speed.
1921

22+
2023
private float _baseMaxSpd;
2124

2225
void Awake()
2326
{
2427
gndCheck = GetComponent<GroundCheck>();
2528
controller = GetComponent<CharacterController>();
29+
stdMovement = GetComponent<HumanoidMoveStd>();
2630
}
2731

2832

2933
// Update is called once per frame
3034
protected void Update()
3135
{
32-
SetCurSpeed(MaxSpd > 0, CurSpd);
36+
CurSpd = GetCurSpdAccel(MaxSpd > 0, CurSpd);
3337
GravityCalc(Gravity);
3438

3539
if (gndCheck.Grounded && !CanJump) // Start jump cooldown once grounded.
@@ -68,7 +72,7 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
6872

6973
private float CurVel => .5f * Vector3.Magnitude
7074
(new Vector3(controller.velocity.x, 0, controller.velocity.z)); // Horizontal velocity.
71-
private float GndSpdMod => (.5f * (gndCheck.GroundSlope * .4f)) * .15f; // Terrain incline/decline speed modifier.
75+
private float GndSpdMod => (.33f * (gndCheck.GroundSlope * .4f)) * .15f; // Terrain incline/decline speed modifier.
7276
private Vector3 moveVel = Vector3.zero; // Direction to move.
7377

7478
private float _minMvSpd = .2f;
@@ -79,47 +83,25 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
7983
/// <param name="mxSpeed"></param>
8084
protected void Move(float mxSpeed)
8185
{
82-
_baseMaxSpd = mxSpeed; // Save cur base max speed before modification.
83-
84-
float newMax = 0;
85-
// Max speed more/less depending on terrain incline.
86-
if (mxSpeed > 0 && controller.velocity.y >= 0)
87-
newMax = _minMvSpd + (mxSpeed + CurVel) - GndSpdMod; // Less top speed if go up incline.
88-
else if (mxSpeed > 0)
89-
newMax = _minMvSpd + (mxSpeed + CurVel) + GndSpdMod; // More top speed if down incline.
90-
91-
// Clamp min/top max speed.
92-
newMax = Mathf.Clamp(newMax, 0, mxSpeed * 1.6f);
93-
94-
// Update max speed if significant difference.
95-
if (Mathf.Abs(_prevMaxSpd - newMax) >= _minMvSpd)
96-
{
97-
if (_baseMaxSpd > 0 && newMax < (jogSpd * .8f)) // Enforce not too slow.
98-
newMax = jogSpd * .8f;
99-
100-
_prevMaxSpd = newMax;
101-
MaxSpd = newMax;
102-
}
86+
// Update max speed for standard movement, and save original base max move speed for acceleration.
87+
MaxSpd = stdMovement.Move(mxSpeed, jogSpd, _minMvSpd, CurVel, GndSpdMod, ref _prevMaxSpd);
88+
_baseMaxSpd = mxSpeed;
10389
}
10490

10591

10692
/// <summary>
107-
/// Accel/Decel for current speed.
93+
/// Returns current speed after applying either Acceleration or Deceleration.
10894
/// </summary>
10995
/// <param name="moving"></param>
11096
/// <param name="curSpeed"></param>
11197
/// <param name="maxBaseSpd"></param>
11298
/// <returns></returns>
113-
private void SetCurSpeed(bool moving, float curSpeed)
99+
private float GetCurSpdAccel(bool moving, float curSpeed)
114100
{
115101
// If in air, limit speed.
116-
if (!gndCheck.Grounded)
117-
{
118-
CurSpd = .2f + (Mathf.Clamp(CurVel * .005f, 0, .015f));
119-
return;
120-
}
121-
122-
// Debug.Log("Max: " + MaxSpd + ", Cur: " + CurSpd) ;
102+
if (!gndCheck.Grounded)
103+
return .2f + (Mathf.Clamp(CurVel * .005f, 0, .015f));
104+
123105
float acclBonus = 0;
124106
// If base max spd < dynamic MaxSpd, then going down slope so increase max spd.
125107
if (_baseMaxSpd <= MaxSpd)
@@ -139,7 +121,7 @@ private void SetCurSpeed(bool moving, float curSpeed)
139121
curSpeed = 0;
140122

141123
// Update applied current speed.
142-
CurSpd = curSpeed;
124+
return curSpeed;
143125
}
144126

145127

HumanoidMoveStd.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class HumanoidMoveStd : MonoBehaviour
6+
{
7+
protected CharacterController controller; // Character environment info getting.
8+
9+
// Start is called before the first frame update
10+
void Start()
11+
{
12+
controller = GetComponent<CharacterController>();
13+
}
14+
15+
16+
17+
/// <summary>
18+
/// Returns new dynamic max speed and updates prevMaxSpd reference with any updates to the maximum speed.
19+
/// </summary>
20+
/// <param name="mxSpeed"></param>
21+
/// <param name="jogSpd"></param>
22+
/// <param name="minMoveSpd"></param>
23+
/// <param name="curVel"></param>
24+
/// <param name="gndSpdMod"></param>
25+
/// <param name="prevMaxSpd"></param>
26+
/// <returns></returns>
27+
public float Move(float mxSpeed, float jogSpd, float minMoveSpd, float curVel, float gndSpdMod, ref float prevMaxSpd)
28+
{
29+
float newMax = 0;
30+
// Max speed more/less depending on terrain incline.
31+
if (mxSpeed > 0 && controller.velocity.y >= 0)
32+
newMax = minMoveSpd + (mxSpeed + curVel) - gndSpdMod; // Less top speed if go up incline.
33+
else if (mxSpeed > 0)
34+
newMax = minMoveSpd + (mxSpeed + curVel) + gndSpdMod; // More top speed if down incline.
35+
36+
// Clamp min/top max speed.
37+
newMax = Mathf.Clamp(newMax, 0, mxSpeed * 1.6f);
38+
39+
// Update max speed if significant difference.
40+
if (Mathf.Abs(prevMaxSpd - newMax) >= minMoveSpd)
41+
{
42+
if (mxSpeed > 0 && newMax < (jogSpd * .8f)) // If moving, enforce not too slow.
43+
newMax = jogSpd * .8f;
44+
45+
prevMaxSpd = newMax;
46+
return newMax;
47+
}
48+
return prevMaxSpd;
49+
}
50+
}

0 commit comments

Comments
 (0)