Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | K1ngK0ng |
extends KinematicBody2D
export (int) var speed = 200
var velocity = Vector2.ZERO
var state_machine
func _ready():
state_machine = $AnimationTree.get(“parameters/playback”)
func get_input():
var current = state_machine.get_current_node()
velocity = Vector2.ZERO
if Input.is_action_pressed(“right”):
velocity.x += 1
state_machine.travel(“RunRight”)
if Input.is_action_pressed(“left”):
velocity.x -= 1
state_machine.travel(“RunLeft”)
if Input.is_action_pressed(“up”):
velocity.y -= 1
state_machine.travel(“RunUp”)
if Input.is_action_pressed(“down”):
velocity.y += 1
state_machine.travel(“RunDown”)
if velocity.length() == 0:
state_machine.travel(“IdleDown”)
func _physics_process(delta):
get_input()
velocity = velocity.normalized() * speed
velocity = move_and_slide(velocity)
Few tips:
You can format your code
in the question by selecting it and pressing the { } button above the text editor. This way others won’t have problem reading it and you’ll get the answer faster.
Also it’s better to specify an exact version instead of “latest”, since latest could mean latest stable release, in development build or something else entirely - also if somebody has the same question few years from now, they’ll have even harder time pinpointing if the answer is suitable for their version of godot.
aXu_AP | 2021-11-04 19:40