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.
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)
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 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