- Home /
RigidBody falls through the ground
I'm working on some swimming physics. I made a script in C# that is applying weak downward force (gravity in water). The script is enabled everytime player is below sea level. When I start the game player falls to the sea. Everything works fine and after hitting the water, script disables character controller scripts and player start to slowly drown. However after hiting the seabed it goes straight down into the void.
I tried to fix that, adding capsule collider to player but then some wierd things started to happen: after hiting play, player goes quickly up, instead of going down.
Does anybody know why?
//this script is enabled only below the sea level
private Vector3 grav;
public float gravity;
void Start()
{
grav = new Vector3 (0.0f,-gravity,0.0f);
distToGround = collider.bounds.extents.y;
}
void Update()
{
rigidbody.AddForce (grav);
}
I have also tried to use raycast to check if player is hitting the seabed but that didn't work too...
bool IsGrounded()
{
return Physics.Raycast (transform.position, -Vector3.up, distToGround + 0.2f);
}
Please help.
How is your ground set up? Does it have a collider? Does your player have a collider? Did raycasting fail (no collision detected?) or did it detect the ground correctly, but the player went through anyway?
just increase the drag of the rigidbody when you hit water because that is what is actually happening
Why don't you use a trigger collider for the water? That way, you can use OnTriggerStay to add your gravity force. You can make a box collider with the volume of the water a trigger and apply a script to add your force using OnTriggerStay.
Your answer
Follow this Question
Related Questions
Why object goes sometimes through walls by adding force ? 2 Answers
Adding Force to a Rigidbody through OnTriggerEnter 1 Answer
Why do I get this error in such a simple script? 3 Answers
Check for rigidbody presence when in collision with a trigger 2 Answers
Jittery movement when colliding (sliding) against curved mesh collider 0 Answers