• Godot Help3D
  • Please help make my player move in the direction it's facing

I've got the player in my first 3D game to move around, have a camera following behind them, and they can turn.

But I can't get the player to move in the direction they are facing (after turn). Can someone please tell me how to change the last three lines of this code?

extends CharacterBody3D

var _speed = 3.0
var _gravity = ProjectSettings.get_setting("physics/3d/default_gravity");

func _physics_process(delta: float):
	handleTurn(delta)
	var input_vector = Vector2(Input.get_joy_axis(0, JOY_AXIS_LEFT_X), Input.get_joy_axis(0, JOY_AXIS_LEFT_Y))
	handleGamepadMove(delta, input_vector)
	handleKeyboardMoveIfNoGamepadInput(delta, input_vector)
	if not is_on_floor():
		velocity.y -= _gravity * delta
	move_and_slide()
	$Camera.look_at(self.position)
	
func handleTurn(delta: float):
	var right_input_x = Input.get_joy_axis(0, JOY_AXIS_RIGHT_X)
	if abs(right_input_x) > 0.1: 
		self.rotation.y += right_input_x * -5 * delta

func handleGamepadMove(delta: float, input_vector: Vector2):
	if input_vector.length() < 0.1:
		input_vector = Vector2.ZERO
	else:
		input_vector = input_vector.normalized()
	var position2 = Vector2(self.position.x * _speed * delta, self.position.z * _speed * delta)
	position2 = position2 * Transform2D(self.rotation.y, input_vector)
	self.velocity = Vector3(position2.x, self.velocity.y, position2.y)
  • xyz replied to this.

    signup Move the player along its basis vectors, not along x and z axes.