How to rotate the player on the x axis with the camera attached moving to the x axis on mouse event?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Elytra

Hello, I am new to Godot.

I tried creating a FPS like third-person movement, I managed to move my character, which is a ball using

if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
	velocity.x=0
elif Input.is_action_pressed("ui_right"):
	velocity.x= SPEED
	$MeshInstance.rotate_z(deg2rad(-ROTATION))
elif Input.is_action_pressed("ui_left"):
	velocity.x = -SPEED
	$MeshInstance.rotate_z(deg2rad(ROTATION))
else:
	velocity.x = lerp(velocity.x, 0, 0.1)
if Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down"):
	velocity.x=0
elif Input.is_action_pressed("ui_up"):
	velocity.z = -SPEED
	$MeshInstance.rotate_x(deg2rad(-ROTATION))
elif Input.is_action_pressed("ui_down"):
	velocity.z = SPEED
	$MeshInstance.rotate_x(deg2rad(ROTATION))
else:
	velocity.z = lerp(velocity.z, 0, 0.1)
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = SPEED
move_and_slide(velocity, Vector3.UP)

And then I added a camroot node (as child) to the main The Ball, and then more 2 child nodes h and v respectively with a ClippedCamera as child of v. Then proceeded with this code:

var escape = false
var camh_rot = 0
var camv_rot = 0

var cam_min = -40
var cam_max = 50

var sensitivity = 0.5
var acceleration = 20
func _ready():
	pass
func _input(event):
	if !escape:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	if event is InputEventMouseMotion:
		camh_rot += -event.relative.x * sensitivity
		camv_rot += -event.relative.y * sensitivity	
	if Input.is_action_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		escape = true
		
		
func _physics_process(delta):
	
	camv_rot = clamp(camv_rot, cam_min, cam_max)
	$h.rotation_degrees.y = lerp($h.rotation_degrees.y, camh_rot, delta * acceleration)
	$h/v.rotation_degrees.x = lerp($h/v.rotation_degrees.x, camv_rot, delta * acceleration)

In here, we are taking an object h which rotates the nodes of camroot, along the y axis while v for the x axis when we move our mouse.

Now, when I rotate my mouse horizontally to the right, my camera moves there or “faces” there as expected, but my movement still remains the same, ie.

I tried using a lot of methods, like accessing the parents from child, some suggested use rotate_y but it just says “rotate_y” is not valid. I cannot think of a workaround for this, please help.

if i understand your code and scene structure correctly then you can use vectors
h.global_transform.basis.x to move left/right and h.global_transform.basis.z to move forwards/backwards

LazyBigCat | 2023-03-06 22:28

I don’t want to move “h” at all, h is already moved by the InputEventMouseMotion, what I want to do is rotate the node, the parent i.e. the ball on its x axis with the same intensity as that of the child nodes i.e. h, v and clipped camera.

So, if h moves by x distance or intensity in one direction, the parent or the ball rotates too, in the same direction of the same intensity

Elytra | 2023-03-07 04:19