First jump image:
Second jump image:
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Move Settings")]
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float normalSpeed = .2f;
[SerializeField] float slowSpeed = .01f;
[SerializeField] float slowAnimationSpeed = .02f;
[Header("CastBox Settings")]
public Vector3 boxsize;
public float maxDistance;
public LayerMask layerMask;
//Cached References
private Rigidbody playerRigidBody;
private Animator playerAnimator;
//Properties
private bool canDoubleJump = false;
private Vector3 jumpInput;
private float moveSpeed;
private enum PlayerState { jumped, doubleJumped, onGround };
PlayerState playerState;
//animationstate
[SerializeField] float PlayerYLimit = 2.85f;
private void Awake()
{
playerRigidBody = GetComponent<Rigidbody>();
playerAnimator = GetComponentInChildren<Animator>();
}
private void Start()
{
moveSpeed = normalSpeed;
ChangePlayerState(PlayerState.onGround);
}
private void Update()
{
UpdateAnimationState();
Move();
GroundCheck();
}
private void UpdateAnimationState()
{
if (transform.position.y > PlayerYLimit)
{
playerAnimator.SetBool("Jump", true);
}
else if (transform.position.y < PlayerYLimit)
{
playerAnimator.SetBool("Jump", false);
playerAnimator.SetBool("DoubleJump", false);
}
}
private void Move()
{
if (Input.GetKey(KeyCode.LeftShift) && GroundCheck())
{
SlowSpeed();
}
else
{
NormalSpeed();
}
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
private void ChangePlayerState(PlayerState newPlayerState)
{
playerState = newPlayerState;
Debug.Log(playerState);
if (playerState == PlayerState.jumped)
{
canDoubleJump = true;
}
else if (playerState == PlayerState.doubleJumped)
{
canDoubleJump = false;
}
else if (playerState == PlayerState.onGround)
{
canDoubleJump = false;
}
}
private void OnJump()
{
if (playerState == PlayerState.onGround)
{
ChangePlayerState(PlayerState.jumped);
Jump();
}
else if (playerState == PlayerState.jumped && canDoubleJump)
{
ChangePlayerState(PlayerState.doubleJumped);
Jump();
playerAnimator.SetBool("DoubleJump", true);
}
}
private void Jump()
{
jumpInput = Vector3.up * jumpSpeed * Time.deltaTime;
playerRigidBody.AddForce(jumpInput, ForceMode.Impulse);
}
private void SlowSpeed()
{
moveSpeed = slowSpeed;
playerAnimator.speed = slowAnimationSpeed;
}
private void NormalSpeed()
{
moveSpeed = normalSpeed;
playerAnimator.speed = 1;
}
bool GroundCheck()
{
var isGrounded = Physics.BoxCast(transform.position, boxsize,
-transform.up, transform.rotation, maxDistance, layerMask);
if (isGrounded)
{
ChangePlayerState(PlayerState.onGround);
return true;
}
else
{
return false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position - transform.up * maxDistance, boxsize);
}
}
I am trying to make a 2.5D platformer endless runner on Unity. As you can see at the code i am trying to create a doublejump system. First Jump and Doublejump works correctly. But then it don't. The jumps after first jump are very short. I printed a massage after every Jump and looks like it doesn't register multiple times at first jump. I can't find a working solution to make this jump system work. If i have to change entire logic i am ok with that. But i still want to learn what i am doing wrong. I added two screenshots of my player with ground and without ground. That red thing behind player is boxcast.

