- Home /
Check if there's nothing above the player?
I want to check if there's something above the player so that they couldn't jump in low ceiling areas (since the player's collision changes height during the animation, it makes the player phase through the ceiling and float up).
I am trying to use raycast to simply enable the reaction on button press if there's no hit:
RaycastHit2D JumpCheck = Physics2D.Raycast(transform.position, Vector2.up, 2f);
if (JumpCheck.collider == null)
{
InAir = true;
jumpState = JumpState.Jumped;
jumped = true;
animator.SetTrigger("Jumped");
jumpState = JumpState.InAir;
}
But it doesn't return null even when there's nothing above. What might be the problem? My project is based on the default 2d platformer template.
Answer by suIly · Nov 30, 2019 at 01:35 PM
Instead of (JumpCheck.Collider), use (JumpCheck.hit.collider), it will return whatever the raycast hit.
It seems like RaycastHit2D doesn't have that option:
Your answer
Follow this Question
Related Questions
Raycast2D not finding the player's collider 1 Answer
2D Platformer - Jump on platforms in Background 1 Answer
Input Axis Vertical is not set up? 1 Answer
Can't get platforming character to jump 2 Answers
2D jumping raycast question 0 Answers