gravity direction in a space ship

Godot Version
4.3

Question
Hey there,

i have a moving CharacterBod3D which itself it’s on a moving spaceship (RigidBody3D), what i would like is the character to move relative to the ship, as it now if i rotate the ship and turn it upside down the character falls.

Thanks!

you need to apply a constant force to the player in the direction you want the gravity to be applied

and won’t it be a problem if the player has to jump ?

no since this is how gravity works. The jump-force has to be noticibaly higher then the gravity-force obviously

1 Like

thank for reply! i will try.
do you think using characterbody3D for player and rigidbody3D for the ship is the right approach ?

depends on how the ship is controlled and what physics it should have

I managed not to drop the player, but I have some problems with movement when the ship is rotated, the character behaves strangely, can someone please help me ?

this is the code for my characterbody3d

extends CharacterBody3D


var speed = 5.0
var gravity = 9.8
var mouse_sensitivity = 0.01
var gravity_direction = Vector3(0, -1, 0)
var rotation_x = 0.0
var rotation_y = 0.0


var movement_input = Vector3()

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

func _process(delta: float):
	handle_movement_input()

func _physics_process(delta: float):
	var parent_body = get_parent()

	if parent_body and parent_body is RigidBody3D:
		var rotated_gravity = parent_body.transform.basis * gravity_direction * gravity

		if not is_on_floor():
			# Directly modify the vertical component of the velocity
			velocity.y += rotated_gravity.y * delta
		else:
			velocity.y = 0  # Reset vertical velocity when on the ground

		
		var local_movement = parent_body.transform.basis * movement_input
		velocity.x = local_movement.x * speed
		velocity.z = local_movement.z * speed

		move_and_slide()

func _input(event: InputEvent):
	if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		
		var mouse_motion = event.relative
		rotate_y(-event.relative.x * mouse_sensitivity)
		$first_pers.rotate_x(-event.relative.y * mouse_sensitivity)
		$first_pers.rotation.x = clampf($first_pers.rotation.x, -deg_to_rad(70), deg_to_rad(70))
		

func handle_movement_input():
	var input = Input.get_vector("A", "D", "W", "S")
	movement_input = transform.basis * Vector3(input.x, 0, input.y).normalized()
	print("Degree: ", get_parent().rotation_degrees,"Movement dir: ",movement_input)
	

	

What do you mean by “behaves strangely”?

i will try to post a video later on, but it happens that the characterbody3d does not move or moves slowly, this starts to happen when the ship is rotated more than 45 degrees, when it is totally upside down (180 degrees) i have no problems

this is what happen, basically at 90 (or -90) degree player dont move

This suggests that something is off with your input_rotation or local_movement. Im not very good with 3d-math. I can only suggest to play around with the rotation of the input or local_movement