3D Set X and Y axis's rotation relative to Z current rotation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FellowGamedev
var MOUSE_SENSITIVITY = 0.005
var rot_x = 0
var rot_x_target = 0

var rot_y = 0
var rot_y_target = 0

var rot_z = 0
var rot_z_target = 0   

func _process(delta):
		transform.basis = Basis()

		rot_x_target = lerp(rot_x_target,rot_x,0.05)
		rot_y_target = lerp(rot_y_target,rot_y,0.05)
		rot_z_target = lerp(rot_z_target,rot_z,0.05)

		rotate_object_local(Vector3(0, 1, 0), rot_x_target)
		rotate_object_local(Vector3(1, 0, 0), rot_y_target)

		if Input.is_key_pressed(KEY_E):
			rot_z -= .01
		if Input.is_key_pressed(KEY_Q):
			rot_z += .01
		rotate_object_local(Vector3(0, 0, 1), rot_z_target)

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):
		if event is InputEventMouseMotion:
			rot_x -= (event.relative.x * MOUSE_SENSITIVITY )
			rot_y -= (event.relative.y * MOUSE_SENSITIVITY )

everything is working as intended until I press Q or E which results in the object rotating along the Z axis(as intended) but the issue here is that if I rotate along the X and Y axis they don’t rotate along the new Z axis.
you can slap this code on a camera node and see for yourself what I mean
any help would be very much appreciated!

:bust_in_silhouette: Reply From: MrEliptik

I’m not entirely sure about what you’re trying to do exactly. I think I’ve managed to get the result your expecting by simply changing

rotate_object_local(Vector3(0, 0, 1), rot_z_target)

to

rotate_z(rot_z_target)

This only fixes the issue in two directions(Forward and back) since rotate_z will cause the object to rotate around the global Z axis, I need it to basically behave like this but in any give direction not just forward and backwards.

rotations - Album on Imgur
Between slides 2 and 3 its the same object just rotated 90 degrees right]

as you can see it doesnt rotated as needed, I think you need to somehow set a new z basis every z rotation or something in the lines of that but I cant get it to work…

rotate_object_local(Vector3(0, 0, 1), rot_z_target)

^this fixes this issue but it doesnt fix the main issue where for example i rotate 90 degrees on the X(move the mouse left) axis and the 90 degrees on the Z axis(press E) but if I move the mouse up in that position the object will rotate UP in the global direction and if I move the mouse left it moves left in the global position which isnt what I want

I still very much appreciate your help!

FellowGamedev | 2021-03-20 21:39