Custom check ground

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");
			}
		}
	}

You must totally delete all of your code.

Game engines ALREADY INCLUDE gravity. You must not implement it yourself.

Just look on the Inspector (click apple-3) of your object that has the rigidbody. Look and click “uses Gravity.” That’s it ! Your job is done. This is the reason people use game engines :slight_smile:

Also, I bet you forgot to have a COLLIDER on the ground. Add a big flat collider to make your ground function as ground.

once again … DO NOT implement gravity, delete all the code you have there. Just click “uses gravity” on the rigidbody! Hope it helps!