Question by
CuriousMooseCaller · Aug 14, 2020 at 04:02 PM ·
movementcharactercontrollerisgrounded
Why does isGrounded sometimes return false even though the character is clearly grounded?
I am new to Unity and tried to add an airdash to my 3D platformer. The airdash uses the same button as the jump command however it can only be triggered midair. Sometimes (I don't know when and why) when the character is grounded he dashes on the ground instead of jumping. Printig out the value for isGrounded returns false eventhough the character has been grounded for several seconds.
My movement code:
if (dashTimeLeft <= 0)
{
float ystore = moveDirection.y;
moveDirection = transform.forward * Input.GetAxisRaw("Vertical") + transform.right * Input.GetAxisRaw("Horizontal");
moveDirection.Normalize();
moveDirection *= moveSpeed;
moveDirection.y = ystore;
if (charController.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y += Physics.gravity.y * Time.deltaTime * gravityScale;
if (!charController.isGrounded && dashAvail)
{
if (Input.GetButtonDown("Jump"))
{
if(Input.GetAxisRaw("Vertical") == 0 && Input.GetAxisRaw("Horizontal") == 0)
{
dashDirection = playerModel.transform.forward;
}
else
{
dashDirection = transform.forward * Input.GetAxisRaw("Vertical") + transform.right * Input.GetAxisRaw("Horizontal");
}
dashDirection.Normalize();
dashDirection *= dashForce;
dashTimeLeft = dashTime;
moveDirection = dashDirection;
print(charController.isGrounded);
Instantiate(dashEffect, transform.position + new Vector3(0f,1f,0f), Quaternion.Euler(playerModel.transform.rotation.eulerAngles.x, playerModel.transform.rotation.eulerAngles.y-180, playerModel.transform.rotation.eulerAngles.z));
dashAvail = false;
}
}
if(!dashAvail)
{
dashAvail = charController.isGrounded;
}
}
else
{
dashDirection = moveDirection;
dashTimeLeft -= Time.deltaTime;
}
Comment
Answer by Willi0_0 · Aug 17, 2020 at 07:55 AM
You're best off checking for the ground yourself using a spherecast. This will give you more control.