← Pixel Dreamer Studio

Spawn a hit effect facing the surface

Take the point and normal from a ray, and turn the effect's +Y to the normal.

Cast a ray with intersect_ray() to get the hit position and normal. Spawn the effect there and set its basis to Basis(Quaternion(Vector3.UP, normal)). An effect that sprays along its +Y axis then sprays out of whatever it hit: a floor, a wall or a ceiling.

Blue particle bursts rising from a floor and orange bursts coming straight out of a wall
The script below in Godot 4.7: two hits on the floor (blue) and two on the wall (orange), 0.4 seconds into the scene.

The code

Put this on a Node3D in your level. Set Effect to a one-shot GPUParticles3D scene that sprays along its own +Y axis, which is Direction 0, 1, 0 in its process material.

extends Node3D
## Spawn a one-shot hit effect where a ray hits a surface, spraying out of that surface.
## The effect scene should spray along its own +Y axis.

@export var effect: PackedScene

func spawn_hit(from: Vector3, to: Vector3) -> GPUParticles3D:
	var query := PhysicsRayQueryParameters3D.create(from, to)
	var hit := get_world_3d().direct_space_state.intersect_ray(query)
	if hit.is_empty():
		return null
	var normal: Vector3 = hit.normal
	var fx := effect.instantiate() as GPUParticles3D
	add_child(fx)
	fx.global_basis = Basis(Quaternion(Vector3.UP, normal))  # turn +Y to point out of the surface
	fx.global_position = hit.position + normal * 0.02  # just off the surface
	fx.finished.connect(fx.queue_free)
	fx.restart()
	return fx

Call it with the start and end of the shot, for example the muzzle and a point 100 units ahead. It returns null when the ray hits nothing. In our test we hit a floor, a wall and a ceiling, and each time the particles moved out of the surface on screen. The effect removed itself when it ended.

Without the rotation

We spawned the same effect at the same points with no rotation. On the wall, most of the particles went up along the wall instead of away from it. On the ceiling, none were visible, because they all went up into it.

Ceilings too

A ceiling's normal points straight down, the exact opposite of +Y. Basis(Quaternion(Vector3.UP, normal)) still turned the effect the right way in Godot 4.7, so one line covers floors, walls and ceilings.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Projectile & Cast VFX for Godot 4 has 41 spell projectiles, each with its cast, flight and hit in one scene.

Want ready-made effects? Try the free Arcane VFX pack or the VFX Collection for Godot 4.