3D Player's Spot Shadow dipping into floor on moving downward

Godot Version

4.2.2

Question

I have a problem trying to create a “spot shadow” effect for my 3D third person character. the mesh dips into the floor as the player falls down, with it moving further into the floor as the player falls faster. Here is the code for it:

func _ready():
	var ray_cast = RayCast3D.new()
	ray_cast.target_position = Vector3(0, -ray_length, 0)  # Correct property name
	ray_cast.collision_mask = 1  # Adjust based on your collision layers
	ray_cast.add_exception(self)  # Add the player to the exceptions
	add_child(ray_cast)
	ray_cast.name = "shadow_ray_cast"
	ray_cast.enabled = true

# this is getting called in _physics_update()
func update_shadow_position():
	if is_on_floor():
		shadow_plane.visible = false
	else:
		shadow_plane.visible = true
		# Get the RayCast3D node
		var ray_cast = $shadow_ray_cast
		# Update the raycast position and force an update
		ray_cast.force_raycast_update()
		if ray_cast.is_colliding():
			shadow_plane.global_transform.origin = ray_cast.get_collision_point()

It does stick to the floor just fine as long as I am moving upward. Not sure how to fix it going downward. Any help would be appreciated

Is your shadow_plane a child of the player? It may not update it’s global_transform.origin as the player falls down. If that’s the case, the player will send it into the floor when falling down since the player’s children follow his position.

Check what your is_on_floor returns when the player is falling. I suspect it’s returning false for some reason.

So i did end up finding a solution to this. The issue was a mix of a few things. it was a child of my player but for whatever reason I put the functionality in my player.gd script causing there to be a z-fighting problem. moving it to its own child node was apart of the solution. Setting a .01 offset from there fixed any clipping in the floor. What I ended up doing after that was to instead use a Decal instead of a quadmesh. It works so much better and does exactly what I wanted with it acting as a spot shadow that tilts and warps with the floor.

All I did was apply the raycast script to the decal with a radial gradiant

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.