Home > Net >  Why will my physics 2D raycast not display or work correctly?
Why will my physics 2D raycast not display or work correctly?

Time:02-13

Basically i wanted to make a raycast for jumping but for some reason the distance of the raycast is wrong and never changes and the position of it is so wrong i cant even seem to find it:

    ray2D = Physics2D.Raycast(transform.position, -Vector2.up, .1f, GroundedLayers);
    Debug.Log(ray2D.distance);
    isGrounded = groundCheck();

Grounded layers do not include the player, distance always returns as 0, grounded is always false and the gizmos line only draws when i open the prefab and not at all in the scene:

enter image description here

here the distance is set to .1f but displays the same as 1f

(EDIT: ok so i checked and for some reason the raycast happens around 0, -1 even tho thats nowhere near the player transform?)

enter image description here

CodePudding user response:

Try Vector2.down instead?

Alternative things to try:

Is the player in the scene the same as the prefab? Increase the distance. Add a new GameObject at the feet of the player, reference that in your script and call the Raycast from there. Is ray2d a RayCastHit2d? Are your ground object tags correct, including all grammer/caps? Try outputting in debug, ray2d.collider.name to see what you're actually hitting. If you're using a rigid body, try rigidbody2d. position instead of transform.position

Do you have the player selected in the hierarchy? This is needed to see the gizmos.

CodePudding user response:

I found that the raycast was detecting an invisible trigger so i had to put it on another layer :)

  • Related