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);
}