A check like in_water = feet.y < water_level flips every time a floating body crosses the line, and a body at the surface crosses it all the time. If going in plays a splash, it plays over and over. In our Godot 4.7 test a body floating at the surface with only 2 cm of jitter went in 157 times in 10 seconds at 60 fps and 374 times at 144 fps. Make it go 0.15 m under the surface to count as in, and 0.15 m above it to count as out: one splash for a jump in, a float and a climb out.
The code
Save it as water_state.gd, make one per swimmer with var water = preload("res://water_state.gd").new(), set water.surface to the water's height, and call water.update(feet.global_position.y) every physics frame. Play the splash on entered and switch to swimming there, and switch back on exited.
extends RefCounted
## In the water or out of it, with a margin on both sides of the surface,
## so a body bobbing at the surface does not flip between the two every frame
## and play a splash each time. Call update() every physics frame.
signal entered
signal exited
var surface := 0.0 # the water's height
var margin := 0.15 # go this far below the surface to enter, this far above it to leave
var in_water := false
func update(y: float) -> void: # y = the height you test, for example the body's feet
if not in_water and y < surface - margin:
in_water = true
entered.emit()
elif in_water and y > surface + margin:
in_water = false
exited.emit()
What we measured
A body floating at the surface for 10 seconds: a slow wave of 0, 5 or 10 cm up and down, plus up to 2 cm of random jitter every frame (the same seed each time). Each line counts how often it went into the water. Two runs gave the same numbers.
- No wave, only the jitter: 157 at 60 fps and 374 at 144 fps with the plain check. 0 with a margin of 0.05 m or 0.15 m.
- A 5 cm wave: 27 and 70 with the plain check. 5 with a 0.05 m margin, once per wave. 0 with 0.15 m.
- A 10 cm wave: 13 and 30 with the plain check. 5 with 0.05 m. 0 with 0.15 m.
- A jump from 2 m above the water to 1.5 m under it, rising back to the surface, bobbing there for 5 seconds on a 5 cm wave, then climbing out: 14 with the plain check, and with a 0.15 m margin 1 in and 1 out.
Why it happens
One line means one state on each side of it, so every tiny move across the line is a change of state. A floating body sits right on that line, and more frames per second means more chances to cross it, which is why 144 fps gave more than twice as many splashes. With two lines, a body between them keeps the state it already had: it stays in the water while it bobs, and it stays out while it stands at the edge. The margin has to be larger than the bob: 0.05 m still let a 5 and a 10 cm wave through once per wave, 0.15 m did not.
What to change
margin: more than the height of your waves and bobbing.surface: move it with the water if the level rises or falls.- A splash for something falling in: splash when it hits the water. Ripples around something floating: ripple rings on the surface.
The splash itself: Water & Splash VFX for Godot 4 has Splash in Aqua, Deep, Swamp and Aqua Large and Footstep Splash in three looks, all one-shots: spawn one at the surface with PDVFX.spawn() when entered fires.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Water & Splash VFX has 60 splash, wave and waterspout effects for Godot 4, with one-shot Splash and Footstep Splash presets to play when a body enters the water.