# Unity: RaycastHit2D continually returns point (0,0)

firstly I'm a bit of a Unity noob so sincere apologies if I've done something dumb.

I'm attempting to use a RaycastHit2D to detect when the player is on a platform. If he is, the point it returns should be passed back and used to set the player's rigidbody transform y coordinate so he sits on top of the platform.

The problem appears to be that the RaycastHit2D is continually returning a point of (0,0), so the player keeps getting reset to (x,0) - which causes an amusing bouncing effect, but not the desired effect.

Here's the code:

if (onPlatform) {

RaycastHit2D walkingOnPlat = Physics2D.Raycast(rb.transform.position,Vector2.down,Mathf.Infinity,8);
platYCollision = walkingOnPlat.point.y;
rb.transform.position = new Vector3(x, platYCollision,-5f);

}


If I can clarify anything please do let me know, and thanks in advance for your help!

## 1 Answer

The problem appears to be that the RaycastHit2D is continually returning a point of (0,0)

This happens if the raycast's origin is already within or in contact with an object. In these cases, the raycast's normal will be opposite to the direction it was cast, and it's result will be zero.

Please take a look at the Physics2D.Raycast documentation; specially this paragraph:

Additionally, this will also detect Collider(s) at the start of the ray. In this case the ray is starting inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated in which case the collision normal returned is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

Secondary notes:

Your raycast's distance should definitely not be set for infinity. Raycasts are relatively expensive, and so it's bad practice and very unrecommended to do it the way you are, currently.

When dealing with raycasts, you should make the parameters (/resulting raycast) "as little as possible to get the job done".

Also, considering you are a self-admitted Unity-noob, and that's a strange way to do things if it's indeed correct, do note that the int for the raycast's Layermask is a layermask's bit-wise flags set, not a layer's index. If the value 8 is not a precalculated bit-mask supposed to represent a set of layers, then it's probably wrong.

Edit: To easily convert from index to layermask, you can simply do the bitwise operation to shift bits left to the correct index position. In this case, that would be 1 << 8. Where 1 would be the binary/bit-wise 00..01, and << 8 shifts that binary 1 8 places to the left, resulting in the correct bit-wise mask: 00..0100000000