I'm making a 2D platformer with Unity. I used this tutorial to write the code but with some changes like using the new Input System. Like the tutorial, I've defined three variables called GroundedRememeber
, JumpPressedRemember
and JumpPressedRememberTime
and basically they work like timers and check if the player leaves the ground and if so, then the player can jump when it is near the ground without the need to touch it. The problem is that these timers are not working and the player can't jump, just clings to the ground. My player controller has no errors but the jumping is not working.
Here's my Player Controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour, PlayerInputActions.IPlayerActions
{
private PlayerInputActions controls;
[SerializeField] LayerMask groundLayers;
private Rigidbody2D rb;
private Animator anim;
private bool facingRight = true;
private Vector2 moveInput;
private Vector2 playerSize;
private float JumpPressedRemember = 0;
[SerializeField] float JumpPressedRememberTime = 0.2f;
private float GroundedRemember = 0;
[SerializeField] float GroundedRememberTime = 0.25f;
[SerializeField] float HorizontalAcceleration = 1;
[SerializeField] [Range(0, 1)] float HorizontalDampingBasic = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenStopping = 0.5f;
[SerializeField] [Range(0, 1)] float HorizontalDampingWhenTurning = 0.5f;
[SerializeField] [Range(0, 10)] float JumpHeight = 5f;
private void Awake()
{
controls = new PlayerInputActions();
controls.Player.SetCallbacks(this);
controls.Enable();
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
playerSize = (Vector2) GetComponent<CapsuleCollider2D>().bounds.size + new Vector2(-0.02f, 0);
}
void PlayerInputActions.IPlayerActions.OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
}
void PlayerInputActions.IPlayerActions.OnJump(InputAction.CallbackContext context) {
switch (context.phase) {
case InputActionPhase.Performed:
JumpPressedRemember = JumpPressedRememberTime;
break;
}
}
void FixedUpdate()
{
if(facingRight == false && moveInput.x > 0){
Flip();
} else if (facingRight == true && moveInput.x < 0){
Flip();
}
}
void Flip(){
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
void Update()
{
Vector2 GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
bool Grounded = Physics2D.OverlapBox(GroundedBoxCheckPosition, playerSize, 0, groundLayers);
GroundedRemember -= Time.deltaTime;
if (Grounded)
{
GroundedRemember = GroundedRememberTime;
}
JumpPressedRemember -= Time.deltaTime;
if ((JumpPressedRemember > 0) && (GroundedRemember > 0)) {
rb.AddForce(Vector2.up * JumpHeight, ForceMode2D.Impulse);
GroundedRemember = 0;
JumpPressedRemember = 0;
}
float HorizontalVelocity = rb.velocity.x;
HorizontalVelocity += moveInput.x;
if (Mathf.Abs(moveInput.x) < 0.01f)
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(moveInput.x) != Mathf.Sign(HorizontalVelocity))
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
HorizontalVelocity *= Mathf.Pow(1f - HorizontalDampingBasic, Time.deltaTime * 10f);
rb.velocity = new Vector2(HorizontalVelocity, rb.velocity.y);
}
}
This is the original code from the tutorial which is using the old Input System:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
LayerMask lmWalls;
[SerializeField]
float fJumpVelocity = 5;
Rigidbody2D rigid;
float fJumpPressedRemember = 0;
[SerializeField]
float fJumpPressedRememberTime = 0.2f;
float fGroundedRemember = 0;
[SerializeField]
float fGroundedRememberTime = 0.25f;
[SerializeField]
float fHorizontalAcceleration = 1;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingBasic = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenStopping = 0.5f;
[SerializeField]
[Range(0, 1)]
float fHorizontalDampingWhenTurning = 0.5f;
[SerializeField]
[Range(0, 1)]
float fCutJumpHeight = 0.5f;
void Start ()
{
rigid = GetComponent<Rigidbody2D>();
}
void Update ()
{
Vector2 v2GroundedBoxCheckPosition = (Vector2)transform.position + new Vector2(0, -0.01f);
Vector2 v2GroundedBoxCheckScale = (Vector2)transform.localScale + new Vector2(-0.02f, 0);
bool bGrounded = Physics2D.OverlapBox(v2GroundedBoxCheckPosition, v2GroundedBoxCheckScale, 0, lmWalls);
fGroundedRemember -= Time.deltaTime;
if (bGrounded)
{
fGroundedRemember = fGroundedRememberTime;
}
fJumpPressedRemember -= Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
fJumpPressedRemember = fJumpPressedRememberTime;
}
if (Input.GetButtonUp("Jump"))
{
if (rigid.velocity.y > 0)
{
rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * fCutJumpHeight);
}
}
if ((fJumpPressedRemember > 0) && (fGroundedRemember > 0))
{
fJumpPressedRemember = 0;
fGroundedRemember = 0;
rigid.velocity = new Vector2(rigid.velocity.x, fJumpVelocity);
}
float fHorizontalVelocity = rigid.velocity.x;
fHorizontalVelocity += Input.GetAxisRaw("Horizontal");
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.01f)
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenStopping, Time.deltaTime * 10f);
else if (Mathf.Sign(Input.GetAxisRaw("Horizontal")) != Mathf.Sign(fHorizontalVelocity))
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingWhenTurning, Time.deltaTime * 10f);
else
fHorizontalVelocity *= Mathf.Pow(1f - fHorizontalDampingBasic, Time.deltaTime * 10f);
rigid.velocity = new Vector2(fHorizontalVelocity, rigid.velocity.y);
}
}
This is the PlayerInputActions
window.
The action type of Move is set to Pass Through and control type is Vector2