How to cast a shadow on the floor in a 2D platformer?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unosami

I’ve been attempting to cast a shadow beneath a bat enemy using a RayCast2D. More specifically:

shadow.position = ground_ray.get_collision_point()

which I assumed would put the shadow directly beneath the bat. Instead I get this: Shadow is being cast onto empty space

Any idea how I can fix this?

If it helps this is what my editor setup looks like: Raycast goes down below the screen height

Edit: It’s occurred to me that the images I posted are not showing up. Any idea how to fix that also?

Edit #2: Fixed! Thanks to exuln!

Here’s your images
enter image description here
enter image description here
They aren’t showing up because you linked the imgur album page and not the image directly.

exuin | 2021-09-09 16:29

:bust_in_silhouette: Reply From: exuin

If you check the docs of RayCast2D, for get_collision_point(), it says

Note: this point is in the global coordinate system.

Therefore you need to convert the point to a local point with Node2D.to_local().

Thank you so much! Both for the ray casting issue and the picture issue. This may be a stupid question, but how did you access the link to the direct image? I tried two different links provided by Imgur but neither worked in the original post.

unosami | 2021-09-12 15:48

Right click, “Open image in new tab” or “Copy image link” and paste it. You should use the direct link to the image, not the album link.

exuin | 2021-09-12 17:09