Fps "lean" mechanic goes in same direction no matter where player is facing

Godot Version

godot 4

Question

I’m really inexperienced with rotational math, why does this only rotate my player towards the same direction regardless of where theyre facing?

if Input.is_action_pressed("leanLeft"):
		leanDir = 1
		rotation.z = lerp(rotation, (transform.basis * Vector3(0 , 0, leanDir)).normalized(), delta * .1).z
	elif Input.is_action_pressed("leanRight"):
		leanDir = -1
		rotation.z =  lerp(rotation, (transform.basis * Vector3(0 , 0, leanDir)).normalized(), delta * .1).z
	else:
		leanDir = 0
		rotation.z = lerp(rotation.z, 0.0, delta * 5)
	
	rotation.z = clamp(rotation.z, deg_to_rad(-30), deg_to_rad(30))

try using lerp_angle for rotational values and only on rotation.z, the entire transform.basis is likely unnecessary.

rotation.z = lerp_angle(rotation.z, leanDir, delta * 0.1)

that’s a much simpler way of doing it. now the question is, how do I make it so the rotation is localized to the player’s axis. right now, if I’m facing the initial direction, it leans left and right, but if I turn 90 degrees it rotates up and down

Is the script on the player object, or some parent node? It sounds like you put that script on a parent node and the part that actually turns, is a child of that node.

the root object for the player is a character body 3d, which is what I’m rotating in this script