Function to check Camera's Orientation Not working Properly

Attention Topic was automatically imported from the old Question2Answer platform.

My project utilizes an Isometric Camera, and am attempting to write a function that checks what direction the camera is pointing and display that as a string. The script to rotate the camera rig is functioning correctly, and rotates the camera in 90 degree increments.

func Check_Orientation():
if rotation_degrees.y == 0 or 360:
Camera_Orientation = “NE”
if rotation_degrees.y == 90 or -270:
Camera_Orientation = “NW”
if rotation_degrees.y == 180 or -180:
Camera_Orientation = “SW”
if rotation_degrees.y == 270 or -90:
Camera_Orientation = “SE”
else:
Camera_Orientation = “???”

This function is called every time the command to rotate the camera is given.

Despite running the function on Ready, the display starts off on the ??? option (It begins at 0, so it SHOULD be displaying Northeast), and will only display Southeast, regardless of what direction the camera is actually pointing.

I’d guess you’re being bitten by a number of issues (floating point equality, general code syntax, and logic). First, it’s NEVER a good idea to use exact equality checks against floating point numbers as such checks will routinely fail.

You could try to replace your equality checks with calls to `is_equal_approx()` instead, which is designed to combat such issues.

For example, rather than this:

``````if rotationdegrees.y == 0 or 360
``````

You use something like:

``````if is_equal_approx(rotation_degrees.y, 0)  or is_equal_approx(rotation_degrees.y, 360):
# do stuff...
``````

(also, note that your `or` syntax (`0 or 360`) isn’t doing what you intend. That needs to be broken into two separate `or` statements.

Docs here:

Just noticed that `else` statement isn’t what you want either as it’s only paired with the last `if` statement… Untested, but something like this might be closer to what you need…

``````func _Check_Orientation():
var rot = rotation_degrees.y
if rot >= 360: rot -= 360
if rot < 0: rot += 360

if is_equal_approx(rot, 0):
Camera_Orientation = "NE"
elif is_equal_approx(rot, 90):
Camera_Orientation = "NW"
elif is_equal_approx(rot, 180):
Camera_Orientation = "SW"
elif is_equal_approx(rot, 270):
Camera_Orientation = "SE"
else:
Camera_Orientation = "???"
``````

jgodfrey | 2023-05-19 17:35

Works great, thanks for the help.

AeromechanicalAce | 2023-05-20 03:48