- Home /
Character Controller is never Grounded
Hi,
So my character controller.isgrounded is never true. ive tried the basic way of just using isgrounded but the controller never is. ive tried using Kurt-Dekker solution on the fourms but that does not work either as the verticle velocity constantly goes down ie its not grounded. i have a very simple character with no colliders just the controller. not sure if this is a bug in unity or something else. here is my current code
private CharacterController controller;
private Transform cam;
public Animator anim;
public bool playerRunning;
public float playerCurrentSpeed = 3.0f;
public float playerSpeed = 3.0f;
public float playerRunSpeed = 5.0f;
public bool canMove;
private Vector3 playerVelocity;
private float verticalVelocity;
private float groundedTimer;
private bool groundedPlayer;
public float jumpHeight = 1.0f;
private float gravityValue = 9.81f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
Vector2 playerMoveDir;
Vector2 playerLookDir;
public Renderer robot;
public AudioSource tracksAudio;
bool Beenhit;
private float nextActionTime = 0.0f;
public float movementSlowPeriod = 2f;
InputMaster input;
// Start is called before the first frame update
private void Awake()
{
cam= Camera.main.transform;
input = new InputMaster();
input.Player.Move.performed += ctx => playerMoveDir = ctx.ReadValue<Vector2>();
input.Player.Look.performed += ctx => playerLookDir = ctx.ReadValue<Vector2>();
input.Player.Run.performed += ctx => playerRunTrigger();
controller = gameObject.GetComponent<CharacterController>();
}
void FixedUpdate()
{
groundedPlayer = controller.isGrounded;
if (canMove)
{
playerMovmentThirdPerson();
}
if (Beenhit)
{
playerCurrentSpeed = playerCurrentSpeed / 2;
if (Time.time > nextActionTime)
{
nextActionTime += movementSlowPeriod;
playerCurrentSpeed = playerSpeed;
Beenhit = false;
}
}
}
void playerMovmentThirdPerson()
{
Vector3 move = new Vector3(0,0,0);
bool groundedPlayer = controller.isGrounded;
if (groundedPlayer)
{
// cooldown interval to allow reliable jumping even whem coming down ramps
groundedTimer = 0.2f;
}
if (groundedTimer > 0)
{
groundedTimer -= Time.deltaTime;
}
// slam into the ground
if (groundedPlayer && verticalVelocity < 0)
{
// hit ground
verticalVelocity = 0f;
}
// apply gravity always, to let us track down ramps properly
verticalVelocity -= gravityValue * Time.deltaTime;
if (input.Player.Jump.triggered)
{
// must have been grounded recently to allow jump
if (groundedTimer > 0)
{
// no more until we recontact ground
groundedTimer = 0;
// Physics dynamics formula for calculating jump up velocity based on height and gravity
verticalVelocity += Mathf.Sqrt(jumpHeight * 2 * gravityValue);
}
}
//Player Moving And turning
Vector3 direction = new Vector3(playerMoveDir.x, 0, playerMoveDir.y);
if (direction.magnitude >= 0.1f)
{
float targetAgnle = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAgnle, ref turnSmoothVelocity,turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAgnle, 0f) * Vector3.forward;
move = moveDirection.normalized;
}
move.y = verticalVelocity;
controller.Move(move * playerCurrentSpeed * Time.deltaTime);
I noticed that you have code copying controller.isGrounded to groundedPlayer on both lines 43 and 63. In the case of 63, it re-initializes the variable. I don't know how that doesn't throw an error itself.
But in your question you said "character controller.isgrounded is never true", so that specific issue isn't in this class anyway. It's on the CharacterController component.
How does that deter$$anonymous$$e isgrounded? The cause could be different based on if it's using a collider or raycast, for example. Most likely, whatever you're using to detect the character's collision with the ground is probably in the wrong place. Make sure it's by the character's feet, and all layers are appropriately assigned.
so im using the default character controller that unity provides. even with a bog standard capsule with movement the isgrounded varaible of the character controller is very rarely true. sometimes it works for a few jumps then it stops working for ages.
with all layers default.