Godot Version
v4.4.stable.official
Question
I want to make a player movement in 0 gravity settings. In the way that forward key would move player in the direction he is facing (even up/down). I chose the rigidBody3D for a player but I have no idea how to rotate the object in the same direction as the camera so all directions don’t mess up when I turn.
Here is the code for movement and a screenshot of player nodes for reference.
extends RigidBody3D
@onready var head: Node3D = $head
@onready var camera: Camera3D = $head/camera
@onready var pointer: Node3D = $head/camera/pointer
const SENSITIVITY = 0.01
var speed = 2
func _ready() -> void:
gravity_scale = 0
func _unhandled_input(event: InputEvent) -> void:
#var direction := (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x,deg_to_rad(-90),deg_to_rad(90))
func _physics_process(delta: float) -> void:
rotation = Vector3(0,0,0)
#angular_velocity = pointer.global_rotation
#rotation = pointer.global_rotation
var direction = pointer.global_rotation
if Input.is_action_just_pressed("forward"):
apply_impulse(-basis.z * speed)
if Input.is_action_just_pressed("back"):
apply_impulse(basis.z * speed)
if Input.is_action_just_pressed("left"):
apply_impulse(-basis.x * speed)
if Input.is_action_just_pressed("right"):
apply_impulse(basis.x * speed)