How do you properly rotate a 3D camera on a spherical surface?

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

Hi, thank you for clicking on my question!

So here is how my code looks like currently:

func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
		Head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))

It works fine on a flat surface, but on a spherical one, the right and the left rotations are gradually switched, while the player walking toward the opposite side.

Here is a video of my problem (It’s hard to demonstrate it well): (I always move the mouse the same amount)

Any help is greatly appreciated!

I am new to Godot but from your description of your problem i would suggest to try reading up on global and local space. there is not really enough here for someone who knows to give a specific answer but on the top of a scene tab there is a button to toggle between global and local space, experimenting with that may be a place to start.

ArthurER | 2020-08-29 21:35

Thank you for your answer! I read up on global and local space, like you said, and tried applying what I knew to my code. I couldn’t get it to work so far, but I’ll be trying some more. I know the button you are talking about, but I’m afraid it only affects the editor, and not the game itself.

UMTomato | 2020-08-30 09:33

:bust_in_silhouette: Reply From: UMTomato

Okay, so it turns out the problem was not what I expected. Here is the solution:

func _input(event):
if event is InputEventMouseMotion:
	rotate_object_local(Vector3.BACK, deg2rad(-event.get_relative().x * mouse_sensitivity))
	Head.rotate_x(deg2rad(-event.get_relative().y * mouse_sensitivity))

The other one didn’t work, because it rotated the player on the global axis.