Skip to content

Commit f1161d7

Browse files
committed
Acceleration and Deceleration.
Humanoids now accelerate and decelerate instead of defaulting to their maximum move speed.
1 parent 926eb4a commit f1161d7

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

Humanoid.cs

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected void Update()
3030
if (gndCheck.Grounded && !canJump) // Start jump cooldown once grounded.
3131
StartCoroutine(JumpCooldown(jumpCooldown));
3232

33+
SetCurSpeed(maxSpd > 0, curSpd, maxSpd);
3334
GravityCalc(Gravity);
3435
ApplyMoveVelocity();
3536

@@ -57,43 +58,61 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
5758
Quaternion.LookRotation(new Vector3(curMoveDir.x, 0, curMoveDir.z)), rotSpeed * Time.deltaTime));
5859
}
5960

60-
private Vector3 moveVel = Vector3.zero;
61+
62+
private float CurVel => .5f * Vector3.Magnitude
63+
(new Vector3(controller.velocity.x, 0, controller.velocity.z)); // Horizontal velocity.
64+
private float GndSpdMod => (.5f * (gndCheck.GroundSlope * .4f)) * .15f; // Terrain incline/decline speed modifier.
65+
private Vector3 moveVel = Vector3.zero; // Direction to move.
66+
67+
6168
/// <summary>
6269
/// Apply move speed to horizontal move velocity.
6370
/// </summary>
6471
/// <param name="speed"></param>
6572
protected void Move(float speed)
66-
{
67-
float curVel = .5f * Vector3.Magnitude(new Vector3(controller.velocity.x, 0, controller.velocity.z));
68-
float gndSpdMod = .5f * (gndCheck.GroundSlope * .4f);
69-
73+
{
7074
// Max speed more/less depending on terrain incline.
7175
if (controller.velocity.y >= 0)
72-
maxSpd = (speed + curVel) - (gndSpdMod * .15f); // Less speed if go up incline.
73-
else maxSpd = (speed + curVel) + (gndSpdMod * .15f); // More speed if down incline.
76+
maxSpd = (speed + CurVel) - GndSpdMod; // Less top speed if go up incline.
77+
else maxSpd = (speed + CurVel) + GndSpdMod; // More top speed if down incline.
7478

7579
// Limit min/top max speed.
7680
if (maxSpd < 0) maxSpd = 0;
7781
else if (maxSpd > speed * 2) maxSpd = speed * 2;
7882

7983
if (gndCheck.Grounded)
80-
moveVel += (transform.forward * maxSpd) * Time.deltaTime;
84+
moveVel += (transform.forward * curSpd) * Time.deltaTime;
8185
}
8286

83-
private float MoveAccel(bool moving)
87+
88+
public float accelMod = 1;
89+
/// <summary>
90+
/// Accel/Decel for current speed.
91+
/// </summary>
92+
/// <param name="moving"></param>
93+
/// <param name="curSpeed"></param>
94+
/// <param name="maxBaseSpd"></param>
95+
/// <returns></returns>
96+
private void SetCurSpeed(bool moving, float curSpeed, float maxBaseSpd)
8497
{
85-
// Cur velocity magnitude -> faster accel, greater incline -> slower accel
86-
curSpd = ( ((curSpd * .25f) + controller.velocity.magnitude) - (gndCheck.GroundSlope * .25f) ) * Time.deltaTime;
87-
88-
// Limit max/min speed.
89-
if (curSpd > maxSpd)
90-
curSpd = maxSpd;
91-
else if (curSpd < 0)
92-
curSpd = 0;
93-
94-
return curSpd;
98+
float acclBonus = 0;
99+
if (maxBaseSpd <= maxSpd) // Down slope - accel faster.
100+
acclBonus += (CurVel + GndSpdMod) / 3;
101+
else acclBonus -= (CurVel + GndSpdMod) / 3;
102+
103+
if (moving)
104+
curSpeed += (accelMod + acclBonus) * Time.deltaTime;
105+
else curSpeed -= (accelMod + acclBonus) * Time.deltaTime;
106+
107+
// Limit min/max speed.
108+
if (curSpeed > maxSpd)
109+
curSpeed = maxSpd;
110+
else if (curSpd < 0)
111+
curSpeed = 0;
112+
curSpd = curSpeed;
95113
}
96114

115+
97116
/// <summary>
98117
/// Appply gravity to vertical move velocity.
99118
/// </summary>
@@ -146,9 +165,11 @@ private IEnumerator JumpCooldown(float cooldown)
146165
/// </summary>
147166
private void ApplyMoveVelocity()
148167
{
168+
if(maxSpd == 0)
169+
{
170+
moveVel.x = 0;
171+
moveVel.z = 0;
172+
}
149173
controller.Move(moveVel);
150174
}
151-
152-
153-
154175
}

PlayerHumanoid.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void Start()
2727
else
2828
Move(jogSpd);
2929
}
30+
else
31+
Move(0); // Idle.
3032

3133
if (Input.GetKey(KeyCode.Space))
3234
Jump();

0 commit comments

Comments
 (0)