Hold the jump button to jump higher with CharacterController
Hi! I have a bit of a problem with jumping in my game. I move my player with CharacterController, so obviously I cant make him jump with Rigidbody. I'm having problems with making my character jump higher when I hold down the jump button. How do I make it so with using CharacterController instead of using Rigidbody?
Answer by Vega4Life · Dec 21, 2018 at 05:05 PM
Here is a starting point for you. The main thing is knowing if you can continue to jump. This is based on an elapsed time against a jump timer. This script allows the user to have a full jump of 0.2 seconds, but can release early if they wish (granting a lower jump height). Also, the user is required to press the jump button again if they wish to jump - but I commented the code so that you can remove it if you wish this not to happen.
using UnityEngine;
public class CharacterControllerJump : MonoBehaviour
{
enum ButtonState { Released, Held };
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float jumpTimer = 0.2f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController player;
float jumpElapsedTime;
ButtonState previousButtonState;
public bool CanJump
{
get
{
return player.isGrounded && previousButtonState == ButtonState.Released;
}
}
public bool CanContinueJump
{
get
{
return jumpElapsedTime <= jumpTimer && player.isGrounded == false;
}
}
void Start()
{
player = GetComponent<CharacterController>();
}
void Update()
{
// If grounded, we set our jump elapsed to zero
if (player.isGrounded) jumpElapsedTime = 0f;
bool jumpButtonPressed = Input.GetKey(KeyCode.Space);
if (jumpButtonPressed)
{
if (CanJump || CanContinueJump)
{
moveDirection.y = jumpSpeed;
jumpElapsedTime += Time.deltaTime;
}
}
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
player.Move(moveDirection * Time.deltaTime);
// We don't need this if the user is allowed to hold and keep jumping
// Most games don't do this, so we have a button state and the user has to hit jump again on landing
previousButtonState = jumpButtonPressed ? ButtonState.Held : ButtonState.Released;
}
}
Hope it gets you going!
Hey, I just used this code in my game and it worked except for a little bug that I noticed, it was allowing the player to double jump, so I added a new variable float to get how many times the player presses the jump button and only allow to continue jumping if they only presses it once.
private float hitTimes = 0f;
void Update()
{
// If grounded, we set our jump elapsed to zero
// set hitTimes to zero
if (player.isGrounded) {
jumpElapsedTime = 0f;
hitTimes = 0f;
}
// register how many time the player pressed the button
if (Input.GetButtonDown("Jump")) hitTimes++;
bool jumpButtonPressed = Input.GetButton("Jump");
if (jumpButtonPressed && hitTimes == 1)
{
if (CanJump || CanContinueJump)
{
moveDirection.y = jumpSpeed;
jumpElapsedTime += Time.deltaTime;
}
}
moveDirection.y = moveDirection.y + (gravity * Time.deltaTime);
player.$$anonymous$$ove(moveDirection * Time.deltaTime);
// We don't need this if the user is allowed to hold and keep jumping
// $$anonymous$$ost games don't do this, so we have a button state and the user has to hit jump again on landing
previousButtonState = jumpButtonPressed ? ButtonState.Held : ButtonState.Released;
}
I hope it can help anyone that had the same bug as me =D
Your answer
Follow this Question
Related Questions
charactercontroller car acceleration and deceleration 0 Answers
How to make the character walk-jump and walk (and resize the collider when crouched) ? 0 Answers
Character's jumping mechanism stuck into something invisible 1 Answer
Predictive Trajectory Issues 0 Answers
Using gravity, CharacterController.isGrounded still is unreliable 0 Answers