- Home /
Raycast2D not working
I'm making a platformer and trying to restrain the character jumps. I decided to make it so that whenever the raycast doesn't detect something you can't jump.
I decided to make the raycast code and make it write whatever it is hitting in the debug log. However, even though my raycastOrigin is floating in space (with a square very far below it) the debug log still says "square".
RaycastHit2D hit = Physics2D.Raycast(raycastOrigin.position, Vector2.down);
Debug.Log(hit.transform.name); Debug.DrawRay(raycastOrigin.position, Vector2.down, Color.green);
Answer by Skinchanger29 · Apr 02 at 09:50 PM
You may need to add a distance to the Physics2D.Raycast. Something like Physics2D.Raycast(raycastOrigin.position, Vector2.Down, 10f). If you don't add a distance by default I think it may be infinite so any square beneath them will be detected. You can change the drawn ray to this by changing Vector2.down to the hit variable. Hope this helps!
Your answer
Follow this Question
Related Questions
Jump only when Jump button is pressed - not held 1 Answer
2d rigidbody jump not working 1 Answer
I need help with fixing my "up special" mechanic. 1 Answer
Long press for charged Jump 1 Answer
Problem with Jump 1 Answer