I need to implement Gravity and check for the ground. Now the player falls but he goes through the ground and keeps on falling. Here is the code:
void FixedUpdate()
{
if(IsGravity)
{
UpdateGravity();
}
if (this.Direction.magnitude > 0f) this.Direction.Normalize();
transform.position += Direction * Speed * Time.deltaTime;
}
void UpdateGravity()
{
RaycastHit hit;
if( Direction.y > -TerminalVelocity)
{
Direction = new Vector3(Direction.x, Direction.y - Gravity * Time.deltaTime,Direction.z);
}
if(Physics.Raycast(transform.position, Vector3.down,out hit) )
{
float distance = Vector3.Distance(transform.position, hit.point);
Debug.Log(distance);
if(distance < 0.02f && Direction.y < -1)
{
Direction = new Vector3(Direction.x, -1,Direction.z);
Debug.Log("ok");
}
}
}