The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I'm trying to change the below code into twin stick movement, where WASD moves left and right, up and down, and the rotation of the sprite is based on where the cursor is.

extends CharacterBody3D

# How fast the player moves in meters per second.
@export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75

@export var jump_impulse = 20

@onready var animation_tree = $AnimationTree
@onready var state_machine = animation_tree.get('parameters/playback')

func _ready():
    animation_tree.active = true

func _physics_process(delta):
    var direction = Vector3.ZERO

    if Input.is_action_pressed("ui_right"):
        direction.x += 1
    if Input.is_action_pressed("ui_left"):
        direction.x -= 1
    if Input.is_action_pressed("ui_down"):
        direction.z += 1
    if Input.is_action_pressed("ui_up"):
        direction.z -= 1

    if direction != Vector3.ZERO:

        animation_tree.set('parameters/Idle/blend_position', direction)

        direction = direction.normalized()

    velocity.x = direction.x * speed
    velocity.z = direction.z * speed
    velocity.y -= fall_acceleration * delta
    move_and_slide()

    if is_on_floor() or is_on_ceiling():
        velocity.y = 0.0

    if is_on_floor() and Input.is_action_just_pressed("jump"):
        velocity.y += jump_impulse

imgur link of what it look like now. Any help is appreciated, thanks!

Godot version 4.0.rc1
in Engine by (12 points)

Got it mostly working: https://imgur.com/a/4f5CNSj

extends CharacterBody3D

# How fast the player moves in meters per second.
@export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
@export var fall_acceleration = 75

@export var jump_impulse = 20

@onready var animation_tree = $AnimationTree
@onready var state_machine = animation_tree.get('parameters/playback')
const IDLE_BLEND_POSITION : String = 'parameters/Idle/blend_position'


func _ready():
    animation_tree.active = true

func _physics_process(delta):
    var direction = Vector3.ZERO

    if Input.is_action_pressed("ui_right"):
        direction.x += 1
    if Input.is_action_pressed("ui_left"):
        direction.x -= 1
    if Input.is_action_pressed("ui_down"):
        direction.z += 1
    if Input.is_action_pressed("ui_up"):
        direction.z -= 1

    if direction != Vector3.ZERO:
        direction = direction.normalized()

    velocity.x = direction.x * speed
    velocity.z = direction.z * speed
    velocity.y -= fall_acceleration * delta
    move_and_slide()

    var Y_SHOOTING_OFFSET = 1.0
    var global_transform = Vector3(0,0,0)
    var shooting_origin = global_transform + Vector3(0.0, Y_SHOOTING_OFFSET, 0.0)
    var screen_mouse_position = get_viewport().get_mouse_position()
    var screen_shooting_position = $Camera3D.unproject_position(shooting_origin)
    var screen_shooting_direction = screen_mouse_position - screen_shooting_position
    var shooting_direction = Vector3(screen_shooting_direction.x, 0.0, screen_shooting_direction.y * sqrt(2)) # sqrt(2) on Y axis because of this particular 45° camera setup
    animation_tree.set(IDLE_BLEND_POSITION, screen_shooting_direction)

    if is_on_floor() or is_on_ceiling():
        velocity.y = 0.0

    if is_on_floor() and Input.is_action_just_pressed("jump"):
        velocity.y += jump_impulse

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.