If statement not working even though position is correct?

Godot Version

4.2

Question

Trying to use position in a statement causes the statement to never run?

I have an idea of another way i could tackle this but I’ve run into this issue before where despite my variables matching what im checking for in my if statements, they never run.

image

entire script for transparancy

extends Camera3D

var tween

var left = "left"
var right = "right"
var back = "back"

var state = "neutral"
var isHeld = false

func _process(_delta):
# inputs
	if Input.is_action_just_pressed(left) and state == "neutral":
		state = "left"
		isHeld = true
		tween = get_tree().create_tween()
		tween.tween_property(self, "position", Vector3(-1,0,0), .2)
	elif Input.is_action_just_pressed(right) and state == "neutral":
		state = "right"
		isHeld = true
		tween = get_tree().create_tween()
		tween.tween_property(self, "position", Vector3(1,0,0), .2)
	elif Input.is_action_just_pressed(back) and state == "neutral":
		state = "back"
		isHeld = true
		tween = get_tree().create_tween()
		tween.tween_property(self, "position", Vector3(0,0,1), .2)
	
	# releases
	if Input.is_action_just_released(left):
		isHeld = false
	elif Input.is_action_just_released(right):
		isHeld = false
	elif Input.is_action_just_released(back):
		isHeld = false
	
	if isHeld == false:
		tween = get_tree().create_tween()
		tween.tween_property(self, "position", Vector3(0,0,0), .2)
	
	if global_position == Vector3(0,0,0) and isHeld == false:
		state = "neutral"

	prints(global_position, isHeld, state)

This is because vector components are floating point numbers, and floating point number comparison is tricky. The value you print is rounded to the nth decimal, so the values appear as identical. But at a greater precision, they’re not. If that makes sense?

Usually, you should use is_equal_approx when comparing floating points (or its counterparts for vector2 and vector3).

3 Likes

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