- Home /
isGrounded is always false, even with gravity, how do you fix that?
How do I make it not be false all the time, i've seen some other people talking that its gravity's vault, but i do already have it implemented velocity.y += gravity * Time.deltaTime;
Here's the code, only isGrounded
isn't working, is there something wrong with the code, or the object that im walking on?
using UnityEngine;
namespace Praktyki.Player
{
public class Movement : MonoBehaviour
{
CharacterController controller;
public float speed = 6f;
public float gravity = -40f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Start ()
{
controller = gameObject.GetComponent<CharacterController>();
}
void Update ()
{
//movement
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
//jump
if (Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//sprint
if (Input.GetKey("left shift")) speed = 12f;
else speed = 6f;
}
}
}
ground check should work regardless if there is gravity or not if you have setup it correctly, make sure the groundcheck object is at the very bottom of the player, the groundMask is properly setup in the inspector and so are the layers on all ground object..(you can find layers option once you click on an object in hiearchy, in the inspector (the right panel) on the top area right side, just besides the tag option)...and try tweaking the value of groundDistance until it works
Also, I think it should be in FixedUpdate() instead of regular Update() because that's where physics checks usually happen.
Well for character controllers, it works totally fine in Update (personal experience)
Answer by Alex_Hosseini · Aug 12, 2021 at 02:46 PM
@krzygogogo use character controller ground detector instead:
CharacterController controller;
if( controller.isgrounded )
{
//do something
}
Your answer
Follow this Question
Related Questions
How to prevent player from moving in air while jumping in place?? 0 Answers
how to move and jump at the same time,how to move and jump at the same time 0 Answers
Problem between my player jump and player movement 1 Answer
Box collider didn't jump with player 1 Answer
I cant make my character jump ?,Why can't he jump ? 0 Answers