- Home /
Disabling gravity when colliding with the terrain?
So I want to disable gravity whenever I touch the ground, because the way I move my player uses velocity and gravity makes moving the player impossible because it just pushes it into the ground constantly so it can't move forward. What I have now sort of works, since it disabled gravity when the player is colliding with the terrain, but for some reason whenever I move it still glitches out, ignores the collision between the player and terrain and the player falls through the bottom of the world like so (VIDEO OF PROBLEM HERE)
void Start()
{
rb = GetComponent<Rigidbody>();
Physics.gravity = new Vector3(0, -4000F, 0);
rb.useGravity = true;
rb.mass = 60;
}
void FixedUpdate()
{
//applies gravity based on if the Player is touching the ground
if (touchGround == true)
{
rb.useGravity = false;
print("Gravity off!");
}
else if (touchGround == false)
{
rb.useGravity = true;
print("Gravity on!");
}
}
void OnCollisionStay(Collision collision)
{
if (collision.gameObject.name == "Terrain" || collision.gameObject.name == "Stadium")
{
touchGround = true;
}
}
void OnCollisionExit(Collision collision)
{
touchGround = false;
}
This is the code I have now, with some unrelated things stripped out obviously. If anyone can explain why this is happening I would be extremely grateful.
Your answer
Follow this Question
Related Questions
Can't get gravity to work proberly (transform) 2 Answers
2D Movement [HELP!] 0 Answers
rigidbody It's going up and bounces when is moving 1 Answer