![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Ythnan |
extends KinematicBody2D
const FRICTION = 500
const ACCELERATION = 500
const MAX_SPEED = 80
enum {
MOVE,
ROLL,
ATTACK,
}
var state = MOVE
var velocity = Vector2.ZERO
onready var animationplayer = $AnimationPlayer
onready var animationtree = $AnimationTree
onready var animationstate = animationtree.get(“parameters/playback”)
func _ready():
animationtree.active = true
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
ROLL:
pass
ATTACK:
move_state(delta)
func move_state(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
input_vector.y = Input.get_action_strength(“ui_down”) - Input.get_action_strength(“ui_up”)
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
animationtree.set("parameters/Idle/blend_position", input_vector)
animationtree.set("parameters/Run/blend_position", input_vector)
animationtree.set("parameters/Attack/blend_position", input_vector)
animationstate.travel("Run")
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
animationstate.travel("idle")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
velocity = move_and_slide(velocity)
if Input.is_action_just_pressed("attack"):
state = ATTACK
func attack_state(delta):
animationstate.travel(“Attack”)
(note: my key for “attack” is spacebar, and j)
You have move_state(delta)
under both MOVE and ATTACK. The attack_state(delta)
function isn’t being called anywhere.
Magso | 2021-01-24 02:23
If I take out move_state(delta) under MOVE, I lose the ability to access my movement animation, and can’t move as well.
Ythnan | 2021-01-24 05:08
You can add a second argument like this move_state(delta, play_anim:= true)
and under ATTACK use move_state(delta, false)
and put all the animation changes under if play_anim:
. However I can’t see anywhere that resets state to MOVE, this can be done by connecting the animation_finished
signal.
Magso | 2021-01-24 10:20