- Home /
Find out that player is not grounded
How can i find out that my player is actually not grounded? Is there any contrary of OnCollisonEnter?
Thanks for the answers.
$$anonymous$$y Character has a Rigidbody2D and I use Rigidbody2D.velocity for movement. The ground is made of BoxCollider2D that may have different rotations. Jumping is not possible.
$$anonymous$$y problem is, that my Player moves away from the platforms when it goes downwards. The player should always be grounded to the lines.
Her is a screenshot of my scene: http://www.directupload.net/file/d/3795/v5jmhlt2_png.htm
you could raycast down from the players feet and if it hits an object tagged ground for example get the hit point and do something with that? not sure but it could be a good starting point.
Answer by fafase · Nov 03, 2014 at 06:39 PM
Since you use 2D you could go for:
[SerializeField] private Layer ground;
[SerializeField] private Transform groundCheckPoint;
bool IsGrounded()
{
Collider2D col = Physics2D.OverlapCircle(groundCheckPoint.position, 0.1f, ground);
return (col != null);
}
The ground check point should be somewhere to the feet.
Answer by b1gry4n · Nov 03, 2014 at 05:53 PM
If you are using a character controller you can use isGrounded
http://docs.unity3d.com/ScriptReference/CharacterController-isGrounded.html
If you are not using a character controller you will have to create your own check as james_170482 pointed out.
"you could raycast down from the players feet..."(I suggest you ray cast from a point above the players feet so that you know for sure it will hit then subtract the height you cast the ray from. Compare the distance to a "maxheightbeforefalling" variable) "...and if it hits an object tagged ground for example get the hit point... "
The great thing about the raycast option is you have the ability to use layermasks with it. Maybe you have certain objects the player can fall through or certain objects you wish to ignore while checking "grounded"
I did not see you were using 2d until now. The methods above (as far as I know, I havent worked with 2d) work with 3d