+3 votes


I'm creating a game where the player can cast spells towards objects that are in front of him. To know which object it will hit, I cast a ray and get the collider. This works.

However, often an object is just a little to the side, and not picked up. The ray is simply too thin. It's frustrating, because you're sure the player is aiming in the right direction, but it's just too hard to reach the object you want.

So, what I need, is to cast a (probably square) shape to cover everything the player should be able to see/hit. The whole area in front of the player.

Is this possible in Godot? Or do I have to, say, use 9 parallel raycasts to cover a square area in front of the player? Or do I have to resort to using an Area with a cube shape?

I know, for example, that Unity has a RayCastSphere function, where you can cast rays to get everything in front of the player between certain angles. Something like that would be great.

Thanks in advance!

in Engine by (18 points)
recategorized by

2 Answers

+1 vote
Best answer

Hi, if you are using KinematicBody2D, you can use the function test_move() to see if a collision would occur. Then you would need to do some raycasts to get the position of the collision. I did this for a teleport, and worked, but i had to use several raycast to cover the area. However, if test_move gave you a collision happened, you know one of the raycast should return a collision also, and if it does no, you should keep trying with other raycasts.

In godot 3.0.6 you cant raycast a shape, but this feature is going tonbe available in godot 3.1 acoording to this but i think they are for a different use..

by (3,491 points)
selected by

I am indeed using a KinematicBody (in 3D). And wow, how have I never encountered the test_move() function before? It's really nice. I don't know if it's any faster, though, than simply adding multiple raycasts and looping through them (checking each of them for collision, and then taking the closest one). Guess I'll just try both techniques and see which one's faster/more accurate.

I also read that article; those raycast shapes are indeed for a different use. Too bad Godot doesn't support the raycast shapes like Unity. But at least there's other ways to do it.

0 votes

I am in a similar situation and I am solving it using Area2D.get_overlapping_bodies()

by (35 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.