Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | okee_dokee |
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!
Got it mostly working: Godot 4.0rc1 Enter the Gungeon movement - Album on Imgur
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
okee_dokee | 2023-02-16 15:21