I’m using a script to control the player character, who has a rigidbody. I don’t want to use the standard character controller because it doesn’t work with a rigidbody, and I want forces to interact with the player.
The issue is that I have been unable to find a reliable method to see if the player is on the ground or not.
One approach I’ve tried is to simply a isGrounded bool that is set to false whenever the player jumps, and is set to true whenever the player collides with anything with the OnCollisionStay method. However, this has poor results. Since isGrounded will be set to be true if anything at all is in contact with the player, the player is able to jump up walls, as long as the player maintains contact with the wall. Additionally, since the isGrounded bool is only set to false when that player jumps, if they player falls of something without jumping, they will be able to jump once in midair.
Another approach I tried is to use raycasting to set the isGrounded bool. I raycasted in Vector3.down direction and the length of slightly more than half of the player’s height every frame. When it contacted anything, I set isGrounded to true, and otherwise set it to false. This had poor results as well. Since the ray cast is a 2 dimensional line, if the player is touching the ground, but not directly where the ray cast is, for example if the player is on the edge of a cliff, isGrounded will be false, resulting in the player being unable to jump. Additionally, since they raycast goes slightly below where the player touches the ground, sometimes isGrounded will not immediately be set to false when the player jumps, since they player hasn’t been jumped far enough away from the ground yet. This can result on the player jumping again on the next frame, making jumps be higher than they should be. I tried modifying the ray casting approach to include multiple ray casts in different areas on the bottom of the player. However, I cannot have an infinite number of ray casts, so there are still situations where the player wasn’t able to jump. It also doesn’t solve the problem of multiple jumps.
Another problem I have is that the player cannot jump while in contact with a wall, likely due to the friction between the player and wall. This can be prevented by making the player be a very low friction material. However, doing so would make the player have low friction towards everything, which would cause the player to slide down hills.
Another approach could be to use the standard character controller, but somehow simulate physics with it. This sounds like a very convoluted approach, and I doubt the results would be very good, although I have yet to try it.
So how can I fix these issues? Any advice would be appreciated.
Also, the Unity Answers gods have been smiting me, resulting in me being unable to comment on anything. Because of this, I need to post new answers in order to reply to anything.
Cheers.