- Home /
The question is answered, right answer was accepted
onGround raycasts return onGround is true while colliding with platform sides
So I've got this character with Rigidbody2D, BoxCollider2D for colliding things and 2 Raycasts on sides to check if onGround is true. There are few platforms in my scene with Box Collider 2D as well. Raycasts are inside my collider so should't collide with platform sides but they do and my player is able to jump because somehow "onGround" is set to true.
This is the code for Raycasts:
onGround = (Physics2D.Raycast(transform.position + colliderOffsetRight, Vector2.down, groundLength, groundLayer)) || (Physics2D.Raycast(transform.position - colliderOffsetLeft, Vector2.down, groundLength, groundLayer));
This is my Jump trigger with jump buffering inside FixedUpdate:
if (jumpTimer > Time.time && onGround)
{
PlayerJump();
Debug.Log(jumpTimer);
}
What could be the problem?
Not sure if I get the issue right, but is it correct that you don't want onGround to be true when the Raycasts collide with the Platforms ?
The only thing I can think of is that your platform is probably on the "groundLayer" so check the layer that is assigned to the platforms perhaps ?
$$anonymous$$y stupid. $$anonymous$$y two Raycasts change their positions on flip(y), so one of them is not inside the collider anymore; That was one problem. But the other side was colliding too, so I had to change collision detection on my player from Discrete to Continuous. That solved it.
Anyway, thanks for being here to help!
Follow this Question
Related Questions
My Raycasts seem to sometimes miss 0 Answers
Raycast bullet collision problem 1 Answer
Unity 2D: Visualise a field of view cone 3 Answers
Raycasting not behaving as expected? 2 Answers
2D Raycast not working 1 Answer