Skip to content

Commit a4ebf65

Browse files
committed
Added jump height based on speed
1 parent 951874e commit a4ebf65

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterControllerV2/DCLCharacterControllerV2.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,7 @@ public void Update(float deltaTime)
168168
lastSlopeDelta = slope * (data.slipSpeedMultiplier * Time.deltaTime);
169169

170170
if (!isGrounded && currentGroundStatus)
171-
{
172-
float deltaHeight = lastUngroundPeakHeight - view.GetPosition().y;
173-
174-
if (deltaHeight > data.jumpHeightStun)
175-
{
176-
lastStunnedTime = Time.time;
177-
isStunned = true;
178-
isLongFall = false;
179-
}
180-
}
171+
OnJustGrounded();
181172

182173
isGrounded = currentGroundStatus;
183174

@@ -203,6 +194,19 @@ public void Update(float deltaTime)
203194
characterState.IsStunned = isStunned;
204195
}
205196

197+
private void OnJustGrounded()
198+
{
199+
accelerationWeight = 0;
200+
201+
float deltaHeight = lastUngroundPeakHeight - view.GetPosition().y;
202+
if (deltaHeight > data.jumpHeightStun)
203+
{
204+
lastStunnedTime = Time.time;
205+
isStunned = true;
206+
isLongFall = false;
207+
}
208+
}
209+
206210
private void UpdateShadowBlob()
207211
{
208212
if (isGrounded)
@@ -392,7 +396,7 @@ private void CalculateVerticalVelocity(float deltaTime)
392396
if (CanJump())
393397
{
394398
characterState.Jump();
395-
float jumpHeight = GetJumpHeight();
399+
float jumpHeight = GetJumpHeight(Flat(lastFinalVelocity));
396400
float jumpStr = Mathf.Sqrt(-2 * jumpHeight * (data.gravity * data.jumpGravityFactor));
397401
velocity.y += jumpStr;
398402
/*var jumpImpulse = new Vector3(velocity.x, jumpStr, velocity.z);
@@ -481,6 +485,7 @@ private void CalculateHorizontalInputVelocity(float deltaTime, float velocityLim
481485
currentAcceleration = data.acceleration;
482486

483487
int targetAccelerationWeight = Mathf.Abs(xAxis) > 0 || Mathf.Abs(yAxis) > 0 ? 1 : 0;
488+
484489
accelerationWeight = Mathf.MoveTowards(accelerationWeight, targetAccelerationWeight, Time.deltaTime / data.accelerationTime);
485490
currentAcceleration = Mathf.Lerp(data.acceleration, data.maxAcceleration, data.accelerationCurve.Evaluate(accelerationWeight));
486491

@@ -519,15 +524,19 @@ private float GetVelocityLimit()
519524
};
520525
}
521526

522-
private float GetJumpHeight()
527+
private float GetJumpHeight(Vector3 flatHorizontalVelocity)
523528
{
524-
return speedState switch
525-
{
526-
SpeedState.WALK => data.jogJumpHeight,
527-
SpeedState.JOG => data.jogJumpHeight,
528-
SpeedState.RUN => data.runJumpHeight,
529-
_ => throw new ArgumentOutOfRangeException(),
530-
};
529+
float maxJumpHeight = speedState switch
530+
{
531+
SpeedState.WALK => data.jogJumpHeight,
532+
SpeedState.JOG => data.jogJumpHeight,
533+
SpeedState.RUN => data.runJumpHeight,
534+
_ => throw new ArgumentOutOfRangeException(),
535+
};
536+
537+
float currentSpeed = flatHorizontalVelocity.magnitude;
538+
float jumpHeight = Mathf.Lerp(data.jogJumpHeight, maxJumpHeight, currentSpeed / data.runSpeed);
539+
return jumpHeight;
531540
}
532541

533542
public CharacterState GetCharacterState() =>

0 commit comments

Comments
 (0)