Using ray casting in 2D game

Hi,

Is it possible to use ray casting via physics.ray_cast function in 2D game and if it is how to do it? I tried using the physics.ray_cast function but I keep getting the “Ray had 0 length when ray casting, ignoring request.” message when only z value differs between start and end vectors of the ray.
I want to use ray cast to find out what game object is selected by a player or to find game objects nearby the selected one.

Thx.

Well, since you are using 2D physics, the z-values are not used. If you wanted to do that, you would just do a 1-pixel ray cast, from (0,0,0) to (0,1,0) for example. However, raycasts only detect the edges of shapes, they won’t detect a hit if they start and end within the same shape.

Instead I suggest you use a small kinematic object and move it to the point you want to check.

2 Likes