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.
↧