Godot Engine

Godot Engine

なかの31 Jan 19, 2024 @ 8:44pm
Issues with Raycasting3d.
Raycasting 3D is frustrating my attempts at finding the position on my generated terrain some objects I'm trying to spawn in it (including the player) should be placed.

Is there any alternative I can use to Raycasting? I tried both the object and the scripted raycasting methods, and both of them seems to not be immediate. Or am I missing something in the code?

Here's the code I'm using right now for getting terrain position:
func GetTerrainAtPosition(X: float, Z: float): $RayCast3D.position = Vector3(X * TerrainRange, TerrainHeight * 2, Z * TerrainRange) $RayCast3D.target_position = $RayCast3D.position + Vector3.DOWN * TerrainHeight * 4 #$RayCast3D.force_raycast_update() if ($RayCast3D.is_colliding()): return $RayCast3D.get_collision_point() print_debug("Didn't found terrain at " + str($RayCast3D.position)) return Vector3(X, 0, Z)

I tried to use force_raycast_update() to see if it could find the collision position, but it doesn't work. It always accudes that didn't found the terrain position.

It is set to collide with bodies and areas, and uses collision layer 1, like the generated terrain does.
Last edited by なかの31; Jan 19, 2024 @ 8:46pm
< >
Showing 1-6 of 6 comments
umop-apisdn Jan 20, 2024 @ 2:23pm 
The code you posted will always print "Didn't found terrain at" and the position of the raycast; that's how you wrote the if statement.

Try this:
if ($RayCast3D.is_colliding()): var collision_point = RayCast3D.get_collision_point() print_debug("Found terrain at " + collision_point) return collision_point else: print_debug("No terrain found by ray from " + str($RayCast3D.position)) return Vector3(X, 0, Z)

You might also want to brush up on Layers vs Masks. The "layer" is where the object lives, the "mask" is what it can see. Make sure the RayCast's mask includes the layer the terrain is on.
Last edited by umop-apisdn; Jan 20, 2024 @ 2:24pm
なかの31 Jan 20, 2024 @ 3:50pm 
I changed the code, and it still doesn't find the terrain.
func GetTerrainAtPosition(X: float, Z: float): $RayCast3D.position = Vector3(X * TerrainRange, TerrainHeight * 2, Z * TerrainRange) $RayCast3D.target_position = $RayCast3D.position + Vector3.DOWN * TerrainHeight * 4 $RayCast3D.force_raycast_update() if ($RayCast3D.is_colliding()): var point = $RayCast3D.get_collision_point() print_debug("Found terrain at " + str(point)) return point else: print_debug("Didn't found terrain at " + str($RayCast3D.position)) return Vector3(X, 0, Z)

And yeah, I set the Raycast layer mask to look for layer 1, which is the same layer of the terrain I'm generating. But still doesn't work.
Romløk Jan 21, 2024 @ 3:41am 
I think the issue is that `target_position` is already relative to the Raycast3D's own position. So since you're manually adding the raycast's position to the value, it may always be pointing off into the sky!

$RayCast3D.target_position = Vector3.DOWN * TerrainHeight * 4

If you turn on "visible collision shapes" in the debug menu, you should be able to see the direction and length of the ray when you run the game. Very useful for debugging strange behaviour like this!
Last edited by Romløk; Jan 21, 2024 @ 3:42am
なかの31 Jan 21, 2024 @ 9:39am 
I had added that out of desperation, but even after removing the raycast position, still doesn't work.
And seems like the ray is here, and shows that it's colliding in-game, but not code wise?
https://imgur.com/a/mdFiDfF
なかの31 Jan 21, 2024 @ 4:25pm 
Well'p. Seems like the Raycast actually manages to find terrain a few frames after the chunks are generated.
I tried making the world generation process be handled by _process() method, and have a delay of at least 1 before going from the step of generating chunks to the step of spawning the player character in the world, and it still doesn't work.

I changed the delay to 5 seconds, and still doesn't work..
なかの31 Jan 21, 2024 @ 5:47pm 
Ok, I managed to find another way of doing this without raycat.
I remembered that my project has a dictionary of vertices that are used by the game, so I used it to get the ground position.

I will only be able to get the position of the ground, but at least it works, and will be handy for placing objects on the map.
Thanks for at least trying to help with this, by the way.

Edit: Many sunflowers, by the way.
https://imgur.com/a/bLL4AhA
Last edited by なかの31; Jan 21, 2024 @ 5:49pm
< >
Showing 1-6 of 6 comments
Per page: 1530 50

Date Posted: Jan 19, 2024 @ 8:44pm
Posts: 6