- Home /
Other
Problem with CharacterController.isgrounded
CharacterController.isgrounded seems to only sometimes return true when I'm moving and always returns false when standing still. Any idea what I'm doing wrong?
[SerializeField] private CharacterController controller;
[SerializeField] private float speed = 12.0f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private float jumpHeight = 3.0f;
private Vector3 velocity;
void Update() {
if (controller.isGrounded && velocity.y < 0) {
velocity.y = 0.0f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(Time.deltaTime * speed * move);
if (controller.isGrounded && Input.GetButtonDown("Jump")) {
velocity.y = Mathf.Sqrt(jumpHeight * -2.0f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
@pucylek Could you perhaps add the code block that you use to detect ground?
I'm using the built-in isGrounded property of the CharacterController game object, what I posted is the whole script.
Answer by Efe_Yilmaz · Mar 03, 2021 at 03:34 PM
The velocity unit is self-defined in its editor. Maybe that's why it didn't work. Maybe change the name or use physics 2D ' s overlapcircle. https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html you can learn it how to solve the problem with overlapcircle.
Thank you for your response, I tried using Physics.OverlapSphere (since I'm working with 3d and not 2d) and it seems that the problem isn't with detecting ground since I still can't jump when standing still. Do you have any idea of what the problem could be?
Did you tried on enter , on stay and on exit methods ?