- Home /
2D Raycasting to detect if a player isGrounded
Hy everybody, In my 2D game plateform, I want to check if my player is on the ground. For this, I use this code in my FixedUpdate() method:
void FixedUpdate(){
RaycastHit2D hit=Physics2D.Raycast(transform.position,-Vector.up,velocity.y);
if(hit.collider!=null){
isGrounded=true;
}
}
As a result, the Raycast function returns my own player collider2D. I've tried with layers but doesn't work (I added a tag and assigned it to the plateform, added the LayerMask.NameToLayer("plateforms") as an argument to my method and I had the same result). Do you know how to send the exact Ray (what are the rights arguments of origins, direction and distance)? Thanks in advance
Answer by Joshdbb · Dec 04, 2014 at 08:02 PM
You need to set the 'Layer' instead of the 'Tag'.
Also if you make your layer mask a public variable then you can set it in the editor using a drop down menu which can be very useful.
Your answer
Follow this Question
Related Questions
How to check if a raycast is not hitting any tagged colliders? 0 Answers
Multiple Cars not working 1 Answer
Using raycast for visible bullet collision (not oncollisionenter) in C# 1 Answer
What's a reliable way to detect if an object is no longer being hit by a ray? 1 Answer
Distribute terrain in zones 3 Answers