- Home /
Getting a smooth jump with the Character Controller
I'm embarrassed to post this here, as there are already several instances of this question on the boards but sadly no proper answers. This is the Update method of my PlayerControl script:
void Update()
{
moveDirectionX = 0f;
moveDirectionY = 0f;
// Check Ground
grounded = Physics.Raycast(transform.position, -transform.up, deltaGround, 1 << LayerMask.NameToLayer("Environment"));
// Check Horizontal Movement
float h = Input.GetAxis("Horizontal");
moveDirectionX += h * moveForce;
// Check Jump
if (Input.GetKeyDown (KeyCode.Space) & grounded) {
jumping = true;
moveDirectionY += jumpForce;
}
// Add Gravity
if (!grounded)
moveDirectionY += gravityForce;
// Move the controller
moveDirection = new Vector3(moveDirectionX * Time.deltaTime, moveDirectionY * Time.deltaTime);
cc.Move(moveDirection * Time.deltaTime);
}
The part commented as // Check Jump
shows how I'm currently handling the jump, basically by adding "JumpForce" to the vertical movement.
This is the method shown in quite a few posts on how to handle a jump with the Character Controller. But as far as I can tell, this is a horrible solution.
After reaching a certain height, the character falls pretty smoothly with the gravity. That is OK.
The problem is getting to that height. The character "teleports" (actually he's not teleporting he's just moving ridiculously fast) to the max height.
How can you get a smooth jump with a CharacterController?
I'm talking a jump that looks decent in like a Mario game.
Answer by invitadoxr · Jun 17, 2020 at 09:45 AM
moveDirection += jumpForce - gravity * Time.deltaTime. this help a litle bit. but maybe is not enought. i searching the same question.,this help a litle bit.
moveDirectionY += jumpForce - gravity * Time.deltaTime
But maybe is not enough. Iam in the search of the same solution.
Answer by FeedMyKids1 · Jun 16, 2020 at 11:58 PM
Honestly, you'll want to just use a RigidBody because it'll take care of everything for you. And you can adjust the gravity in the ProjectSettings.Physics tab.
Here you create an object with a RigidBody attached and use RigidBody.AddForce method and provide a (direction * force) and a ForceMode (Impulse is best for jumping).
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class TestCharContrl : MonoBehaviour { private Rigidbody rb = null;
[SerializeField]
private float horizSpeed = 5.0f;
[SerializeField]
private float jumpPower = 10.0f;
private bool grounded = true;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
//If on the ground and not moving up or down
if (!grounded && rb.velocity.y == 0)
grounded = true;
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
float horizInput = Input.GetAxis("Horizontal");
if (horizInput != 0)
rb.AddForce(Vector3.right * horizInput * horizSpeed, ForceMode.Force);
}
}
If your character is something like a cube and it's doing a bunch of flips/rolls, go to the RigidBody component and open the Constraints dropdown and freeze its Z rotation.
Answer by FrameXdropProvrex · Jun 17, 2020 at 05:57 PM
I solved it by already calculating the gravityforce on the vertical speed while going up. I hope this helps:
//Lets the player be able to jump when grounded.
if (characterController.isGrounded)
{
verticalSpeed = -1;
if (Jump.state)
{
verticalSpeed = JumpForce;
}
}
//Calculating gravity so the player can fall off objects and returns to earth after a jump.
verticalSpeed -= GravityForce * Time.deltaTime;
movement.y = verticalSpeed;
characterController.Move(movement * Time.deltaTime);
Your answer
Follow this Question
Related Questions
How to make smooth jump? 1 Answer
2D box collider (character floating) 1 Answer
Unity my player does not jump 1 Answer
(Solved) Character Floats After Hitting Head on Object 1 Answer
Jumping Sideways 1 Answer