- Home /
My character controller is quite unresponsive, help pls
I recently got into Unity and I'm making my own controller for my 2D game, it works alright but the jump is very unresponsive. Like, it maybe jumps 5 out of 10 times when pressing the space button and I have no idea why. I'm using the built in Character Controller and the IsGrounded check. Any help is appreciated, thanks!
[RequireComponent(typeof(CharacterController))] public class PlayerController : MonoBehaviour {
private CharacterController controller;
private int speed = 12;
private Vector2 moveVector;
private float verticalVelocity;
private float gravity = 1;
public Transform jumpPos;
private void Start ()
{
controller = GetComponent<CharacterController>();
}
private void Update ()
{
Movement();
}
private void Movement()
{
float inputDirection = Input.GetAxis("Horizontal") * speed;
if (controller.isGrounded)
{
verticalVelocity = 0;
if (Input.GetKeyDown(KeyCode.Space))
{
controller.transform.position = jumpPos.transform.position;
}
}
else
{
verticalVelocity -= gravity;
}
moveVector = new Vector2(inputDirection, verticalVelocity);
controller.Move(moveVector * Time.deltaTime);
}
Without knowing what CharacterController
is, we can't really do much here. Since its .isGrounded
flag is the only thing of note here, are you sure the block within that if
statement is executing each time you expect the character to be "grounded"?
Answer by Epicepicness · Jul 02, 2018 at 08:16 PM
I've had similar issues with Unity's build-in Character Controller (eventually leading to me making a custom one instead). The reason it's unresponsive is because the CharacterController.isGrounded isn't reliable and flips between true/false practically every frame. This means that it'll only jump if it's true on the exact frame you push the jump key. While I'm not entirely sure why this happens, it seems to be a result from Unity's collider 'pushing' you up as to not be stuck in the surface.
So I don't have an exact answer for you, but after some googling you could try to following:
you can try making the jump input a GetKey, rather than a GetKeyDown. This would mean the jump is sometimes one frame late, however.
Replace "verticalVelocity = 0;" with "verticalVelocity = -gravity * Time.deltaTime;"
Call the Controller.Move() function twice (first use seems to fix, second to actually move). Not optimal. by this logic you could apply the Move() before checking the grounded, and if you should jump or not?
I think the second option is the best one to try first, and see if it solves anything. Hope this was helpful. :)