3D rigidbody movement around a sphere in Godot4

Yeah, you seem to have figured out most of your problems. Great job!

However, as you mention, you’re experiencing that your movement vector is not oriented correctly. This is due to how you compute the direction vector in _get_oriented_input(). Currently, your direction is analogous to the camera’s basis vectors scaled by the input_dir vector. This is a solid approach if what you are creating is a flying/swimming character that moves in the direction you’re looking. However, since your character is supposed to be grounded, you should not be basing its directional movement solely on the camera’s orientation.

You have to think about the conceptual nature of the system you want to create. In your case, you want to create a system that allows a character to move along the gravitational plane i.e. move on the surface of a planet. The data that defines this plane is, for a perfect sphere, the gravity vector (gravity_direction).

As such, the goal you should have is to compute a directional vector that travels along this gravitational plane. So… how do you do that? The answer is to use vector projection – like so:

	var camera_basis = camera.global_basis
	var ground_normal = -gravity_direction

	# Movement basis vectors
	var direction_z = -camera_basis.z.slide(ground_normal).normalized()
	var direction_x = direction_z.cross(ground_normal).normalized()
	# NOTE: The camera's x-basis is not being utilized here since we cannot...
	# ...guarantee that it aligns with the gravitational plane i.e. the movement...
	# ...plane expected from the character's relation to the ground.
	# ALTERNATIVELY: Project the camera's x-basis onto the ground plane in the same
	# way it was done with the z-basis.

	var dir = (direction_x * input_dir.x + direction_z * input_dir.y).normalized()

Hopefully this helps you solve the problems you’re experiencing regarding your movement direction. Let me know how it goes. Should you have any questions, please post them.

Relevant documentation:

1 Like