Skip to content

Commit

Permalink
Fixed issues with coyote time and max jump count on platformer contro…
Browse files Browse the repository at this point in the history
…ller
  • Loading branch information
DiogoDeAndrade committed Feb 22, 2025
1 parent 4482d1b commit 9e77ee8
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 60 deletions.
57 changes: 26 additions & 31 deletions Assets/OkapiKit/Scripts/Movement/MovementPlatformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine.UIElements;
using static OkapiKit.MovementPlatformer;
using System;
using Codice.Client.Common.GameUI;

namespace OkapiKit
{
Expand Down Expand Up @@ -107,13 +108,14 @@ public enum GlideBehaviour { None = 0, Enabled = 1, Timer = 2 };
private bool prevJumpKey = false;
private float jumpBufferingTimer = 0.0f;
private float jumpTime;
private float coyoteTimer;
private bool actualIsGrounded;
private float lastGroundTime;
private bool isTouchingGround;
private float glideTimer = 0.0f;
public bool isGliding { get; private set; }

const float epsilonZero = 1e-3f;

private int jumpsRemaining => maxJumpCount - currentJumpCount;
public override Vector2 GetSpeed() => speed;
public override void SetSpeed(Vector2 speed) { this.speed = speed; }

Expand Down Expand Up @@ -370,6 +372,10 @@ protected override void CheckErrors()
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Need to define air collider (collider used when character is not on the ground)!", "Objects can have different colliders while in the air and on the ground.\nFor example, it's common to have a box collider while in the air, while having a capsule collider on the ground (better to go up ramps, for example).\nIf we want this behaviour, we need to set the air and ground colliders."));
}
if ((jumpBehaviour != JumpBehaviour.None) && (maxJumpCount == 0))
{
_logs.Add(new LogEntry(LogEntry.Type.Warning, "Max jump count is equal to zero!", "You have jumping enable, but max jump count is set to zero, which makes it impossible to jump - set it to at least 1!"));
}
if (jumpInputType == InputType.Button)
{
CheckButton("Jump button", jumpButton);
Expand Down Expand Up @@ -443,11 +449,12 @@ void FixedUpdate()
UpdateGroundState();

// Jump buffering
if ((jumpBehaviour != JumpBehaviour.None) && (jumpBufferingTimer > 0))
if ((jumpBehaviour != JumpBehaviour.None) && (jumpBufferingTimer > 0) && (maxJumpCount > 0))
{
jumpBufferingTimer -= Time.fixedDeltaTime;
if (isGrounded)
if (isTouchingGround)
{
currentJumpCount = maxJumpCount;
Jump();
}
}
Expand All @@ -462,7 +469,7 @@ void FixedUpdate()

if ((isJumpPressed) && (!prevJumpKey))
{
if ((isGrounded) && (currentJumpCount == maxJumpCount))
if ((isGrounded) && (currentJumpCount == maxJumpCount) && (maxJumpCount > 0))
{
Jump();
}
Expand All @@ -483,11 +490,11 @@ void FixedUpdate()
{
jumpBufferingTimer = jumpBufferingTime;

if ((isGrounded) && (currentJumpCount == maxJumpCount))
if ((isGrounded) && (currentJumpCount == maxJumpCount) && (maxJumpCount > 0))
{
Jump();
}
else if (currentJumpCount > 0)
else if ((currentJumpCount > 0) && (currentJumpCount < maxJumpCount))
{
Jump();
}
Expand Down Expand Up @@ -546,7 +553,6 @@ void Jump()
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x, speed.y);
jumpBufferingTimer = 0.0f;
coyoteTimer = 0;
jumpTime = Time.time;
currentJumpCount--;
}
Expand Down Expand Up @@ -594,11 +600,6 @@ void Update()
{
if (!isMovementActive()) return;

if (coyoteTimer > 0)
{
coyoteTimer -= Time.deltaTime;
}

float deltaX = 0.0f;

UpdateGroundState();
Expand Down Expand Up @@ -626,7 +627,7 @@ void Update()
}

// Need to check with actual is grounded or else coyote time will make the jump count reset immediately after flying off
if (actualIsGrounded)
if (isTouchingGround)
{
rb.gravityScale = 0.0f;
currentJumpCount = maxJumpCount;
Expand All @@ -649,7 +650,7 @@ void Update()
if (absoluteHorizontalVelocityParameter != "") animator.SetFloat(absoluteHorizontalVelocityParameter, Mathf.Abs(currentVelocity.x));
if (verticalVelocityParameter != "") animator.SetFloat(verticalVelocityParameter, currentVelocity.y);
if (absoluteVerticalVelocityParameter != "") animator.SetFloat(absoluteVerticalVelocityParameter, Mathf.Abs(currentVelocity.y));
if (isGroundedParameter != "") animator.SetBool(isGroundedParameter, actualIsGrounded);
if (isGroundedParameter != "") animator.SetBool(isGroundedParameter, isTouchingGround);
if (isGlidingParameter != "") animator.SetBool(isGlidingParameter, isGliding);
}

Expand Down Expand Up @@ -699,31 +700,25 @@ void UpdateGroundState()
int n = Physics2D.OverlapCollider(groundCheckCollider, contactFilter, results);
if (n > 0)
{
actualIsGrounded = true;
isTouchingGround = true;
isGrounded = true;
lastGroundTime = Time.time;
return;
}
else
{
actualIsGrounded = false;
if (rb.linearVelocity.y > 0)
{
coyoteTimer = 0;
}
isTouchingGround = false;
}
}

if (actualIsGrounded)
if ((coyoteTime > 0) && (!isTouchingGround))
{
coyoteTimer = coyoteTime;
}

actualIsGrounded = false;

if (coyoteTimer > 0)
{
isGrounded = true;
return;
float elapsedTime = Time.time - lastGroundTime;
if (elapsedTime < coyoteTime)
{
isGrounded = true;
return;
}
}

isGrounded = false;
Expand Down
2 changes: 1 addition & 1 deletion Assets/OkapiKit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.videojogoslusofona.okapikit",
"displayName": "OkapiKit",
"version": "1.17.2",
"version": "1.17.3",
"unity": "6000.0",
"description": "OkapiKit is a toolkit for creation of simple games without code.",
"keywords": [ "kit" ],
Expand Down
Loading

0 comments on commit 9e77ee8

Please sign in to comment.