Community losfroger

Godot 3.4 calculate input direction via Input.get_vector

Not really a question, just a small tip for people watching this and using Godot 3.4. You can use the new Input.get_vector method to get the input direction. There are some adjustments you'd need to do, since the method returns a Vector2, but here's how I did it:

You need to multiply the look_at by -1 to flip it and make it face the correct way

extends PlayerState

export var max_speed: = 50.0
export var move_speed: = 50.0
export var gravity: = -80.0
export var jump_impulse: = 25.0

var velocity: = Vector3.ZERO

func physics_process(delta: float) -> void:
var input_direction: = Input.get_vector("move_left", "move_right", "move_front", "move_back")

var move_direction: = Vector3(input_direction.x, 0.0, input_direction.y)
if move_direction.length() > 1.0:
move_direction = move_direction.normalized()

if move_direction:
player.look_at(player.global_transform.origin + move_direction * -1, Vector3.UP)

velocity = calculate_velocity(velocity, move_direction, delta)
velocity = player.move_and_slide(velocity, Vector3.UP)

func calculate_velocity(velocity_current:Vector3, move_direction: Vector3, delta: float) -> Vector3:
var velocity_new: = velocity_current

velocity_new = move_direction * delta * move_speed

if velocity_new.length() > max_speed:
velocity_new = velocity_new.normalized() * max_speed

velocity_new.y = velocity_current.y + gravity * delta

return velocity_new

• Nathan Lovato replied

Thanks for sharing!