Reputation: 11
Friends, I have been studying Unity for only 3 months. I need to create a 3D platformer as a test case to get the job. And already at the initial stage, I ran into the problem that I cannot properly check isGrounded. I made it like this:
public bool IsGrounded ()
{
if (Physics.Raycast (transform.position, Vector3.down, colPlayer.bounds.extents.y + 0.1f))
return true;
return false;
}
The fact is that I have many small platforms that touch each other, each of them has its own collider. And when a character passes the border of these platforms, sometimes he loses the isGrounded state. I solved this problem by making a common collider for these platforms.
But then, rope bridges appeared in the game, which sag a little. Because of this, gaps appeared between the platforms. Here it is already impossible to assign a common collider to them. I partially solved the problem by increasing the area of each collider so that they touch each other and penetrate one into one. This almost solved the problem, the state isGrounded on the bridge is rarely lost. But still it gets lost. In addition, the platforms do not behave very naturally due to the density of colliders. For example, if you jump on them from a height, colliders begin to push each other out. So I am looking for another way to determine the isGrounded state, maybe there is some more complex, but effective method?
I tried making two empty objects at the player's legs and directing a raycast from them, and doing isGrounded false only if both legs are false. But for some reason it works even worse on bridges. I could not find any other ways. Can someone please help? Thanks.
Upvotes: 0
Views: 63
Reputation: 625
It is best to check for is grounded with Physics.BoxCast
. It checks the ground for a wider range and usually prevents these types of errors.
https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html
Upvotes: 0
Reputation: 81
Have you heard of a popular concept called Coyote Time
Jump still triggered a few frames after walking off the ledge.
this helps solve the issue of human's perceiving a still being on the ground and makes the game more fun.
if you introduced 1/6th of a second of Coyote Time it might solve your issue and make the game more entertaining.
public static JumpState
{
int framesOffGround = 0;
// run this when you want to check if you can jump
// checks to see if you've been off the ground for 6 frames
public bool CanJump()
{
return = (framesOffGround <= 6);
}
// run this every frame
// checks if you are on or off of the ground and updates framesOffGround
public void IsGrounded ()
{
if (Physics.Raycast (transform.position, Vector3.down, colPlayer.bounds.extents.y + 0.1f))
framesOffGround = 0;
else
framesOffGround++;
}
}
this will only work assuming you have 60FPS so I would integrate Time.DeltaTime too.
I also suspect your issue comes from your character moving along the arc of the hanging bridge and falling. Therefore leaving the ground for a split second you could try increasing your raycast length
Upvotes: 0