Gdscript can't detect direction

I’m making a 2D platformer but you can rotate to move on the z axis in 3d. Part of what I would need is to detect the rotation of the character to move on the z axis when facing in that direction.

The code I’m using to rotate and try detecting it looks like

func _physics_process(delta: float) -> void:
	#rotation script
	if Input.is_action_just_pressed("Turn Left"):
		player.rotate_y (90*PI/180)
	if Input.is_action_just_pressed("Turn Right"):
		player.rotate_y (-90*PI/180)
	if player.rotation_degrees == Vector3(0, 0, 0) or Vector3(0, PI, 0):
		print("test")

but even though the player and children rotate, the second script keeps printing “test”, even after also trying to detect the rotation of the camera_3d node. Does anyone know why this happens and any possible fixes?

Generally it’s a bad idea to compare floating point numbers such as Vector3 with ==. On top of that the or keyword doesn’t know you intend to compare the right side by equality with player.rotation_degrees, the left and right side of or are evaluated completely independently, and Vector3(0, PI, 0) is non-zero so it evaluates to true, it’s similar to if you wrote this:

if player.rotation_degrees == Vector3(0, 0, 0):
		print("test")
if Vector3(0, PI, 0):
	print("test")

Which doesn’t make much sense.

Also worth noting that rotation_degress.y would not return PI in this case, but rotation.y could, or something very close to PI which is why == is bad for floating point numbers, often they are 1.000001 instead of 1.0.

3 Likes