How to make jumping more responsive?

Currently I have a script for my player object that allows it to jump and double jump when pressing the spacebar. However, I have noticed that the jumping is inconsistent. Sometimes the jump will happen as it should. Sometimes it will have significant lag. The double jump suffers from the same problems. Does anyone have advice on how I can make the jumping more consistently responsive?

Here is where the spacebar is checked for input as well as the arrow keys:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PlayerMove))]
public class PlayerControl : MonoBehaviour {

	private PlayerMove player;
	private bool jumpPressed;
	
	void Awake () 
	{
		player = GetComponent<PlayerMove>();
	}

	void Update()
	{
		jumpPressed = Input.GetButtonDown("Jump");
	}
	

	void FixedUpdate () 
	{
		float horizontalMovement = Input.GetAxis("Horizontal");
		player.Move(horizontalMovement, jumpPressed);

		jumpPressed = false;
	}
}

Here is where the movement actually occurs:

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

	public float speed = 5.0f;
	public float jumpForce = 400f;
	public float jumpForceAdd= 50.0f;
	bool doubleJump = false;

	Transform groundCheck, ceilingCheck;
	bool onGround = false;
	[SerializeField] LayerMask groundThings;

	float groundCheckRadius = 0.2f;
	float ceilingCheckRadius = 0.02f;

	[SerializeField] bool moveInAir = true;

	void Awake()
	{
		groundCheck = transform.Find("GroundCheck");
		ceilingCheck = transform.Find("CeilingCheck");
	}

	void FixedUpdate () 
	{
		onGround = Physics2D.OverlapCircle(groundCheck.position,
          groundCheckRadius, groundThings);

		if(onGround)
		{
			doubleJump = false;
		}

	}

	public void Move(float horizontalMove, bool jumpPressed)
	{
		if(onGround || moveInAir)
		{
			rigidbody2D.velocity = new Vector2(horizontalMove * speed,
                  rigidbody2D.velocity.y);
		}

		if((onGround || !doubleJump)&& jumpPressed)
		{
			rigidbody2D.AddForce(new Vector2(0f, jumpForce));

			if(!onGround && !doubleJump)
			{
				doubleJump = true;
			}
		}

	}
}

Hi PacNinja,

My experience AddForce( ) is that it is a little bit unpredictable when you are looking for very specific controls. It works good if you want to add an acceleration over some period of time, but it seems like instantaneous accelerations are a bit iffy.

My solution has been to use velocity for crisp controls instead. Maybe others can provide insight in using AddForce() in a crisp manner.

Your jump code could simply be:

rigidbody2D.velocity = new Vector2( rigidbody.velocity.x, jumpVelocity);

This is basically what you have done for your horizontal movement, set a new x velocity and preserved the existing y velocity. As I mentioned by experience is movement is more crisp in velocity rather than acceleration.

Hey Pac,

it should be easy to fix, use Update() instead of FixedUpdate(). As you normally have more calls of Update() then the other, so you would want to catch your input there to have the chance to make an action in any next frame instead only every 0.02 (you can set that in the settings, value is differing) seconds.

PS: What my above poster said sounds pretty legit also, you should go with your own system to get maximal responsiveness. If you want to be able to do things like wallruns, climbing and whatnot, you’ll have no other way anyway.

Hope that helps,

WRZ