extends CharacterBody3D

var speed = 20
@onready var accel = 7

var direction = Vector3()
var movement = Vector3()

func _physics_process(delta):
    ##get keyboard input for moving
    direction = Vector3.ZERO
    var h_rot = global_transform.basis.get_euler().y

    var f_input = Input.get_action_strength("Backward") - Input.get_action_strength("Forward")
    var h_input = Input.get_action_strength("Right") - Input.get_action_strength("Left")

    direction = Vector3(h_input, 0, f_input).rotated(Vector3(0, 1, 0), h_rot).normalized()

    #make it move
    velocity = velocity.lerp(direction * speed, accel * delta)

    move_and_slide()

Sooo, WASD is already based on the direction, but I want to make controls so that you can fly up and down, but I don't know how to make this also directional. If you dont know what I mean, if you look down, and press up, it should move forward, but if you look down and press forward (W), it should move down.

  • spacecloud replied to this.
  • You need to use the Basis. Basis is basically a coordinate frame, sort of like the x/y/z arrows. By default (if you haven't rotated anything on parents or children) the negative Z axis is forward. So you can use the Basis Z as forward (or you can use X for right, whatever). You don't need to rotate anything, because the basis is always in the current orientation.

    Here is some code from one of my projects.

    # at the top
    var forward_vec = Vector3.FORWARD
    var right_vec = Vector3.RIGHT
    var forward = Vector3()
    var right = Vector3()
    
    # in the function before you move things
    var basis = cam.get_transform().basis
    forward = basis.xform(forward_vec)
    right = basis.xform(right_vec)

    Container Because you used inline(`) code formatting for a code block(```). I'll edit the OP and fix it, you can then try editing the post to see how I've formatted it for you. You can also use ~~~ instead if that is more convenient for you.

      Container
      You need to transform the direction by your rotation.

      Click to reveal Click to hide
      direction = global_transform.basis * Vector3(
          x_input,
          y_input,
          z_input)

      @spacecloud

      But how to make it for the x rotation? Now, it only works with the z rotation, so it only works when I tilt the camera on the z axis.

      You need to use the Basis. Basis is basically a coordinate frame, sort of like the x/y/z arrows. By default (if you haven't rotated anything on parents or children) the negative Z axis is forward. So you can use the Basis Z as forward (or you can use X for right, whatever). You don't need to rotate anything, because the basis is always in the current orientation.

      Here is some code from one of my projects.

      # at the top
      var forward_vec = Vector3.FORWARD
      var right_vec = Vector3.RIGHT
      var forward = Vector3()
      var right = Vector3()
      
      # in the function before you move things
      var basis = cam.get_transform().basis
      forward = basis.xform(forward_vec)
      right = basis.xform(right_vec)

        You can use forward as the direction:

        velocity = velocity.lerp(forward * speed, accel * delta)

        Check the API, I got it to work but I don't remember how off hand.