I want run to work
Asked Answered
H

2

0

I want run to work but it only works when run is already pressed and then there is movement if there is movement first it wil do the running animation standing in one spot and not budging what should i do or is there a simple fix please anyone help me

extends KinematicBody2D

export var ACCELERATION = 500
export var MAX_SPEED = 80
export var ROLL_SPEED = 111.456
export var FRICTION = 500
export var DASH_SPEED = 105.8

enum {
MOVE,
ROLL,
ATTACK,
RUN,
DASH
}

var state = MOVE
var velocity = Vector2.ZERO
var roll_vector = Vector2.DOWN
var run_vector = Vector2.ZERO

onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")

func _ready():
randomize()
animationTree.active = true

func _physics_process(delta):
match state:
MOVE:
move_state(delta)

	ROLL:
		roll_state()
		
	
	ATTACK:
		attack_state()
	
	RUN:
		run_state()
	
	DASH:
		pass

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:
	roll_vector = input_vector
	run_vector = input_vector
	# Set animation blending based on the movement direction
	animationTree.set("parameters/Idle/blend_position", input_vector)
	animationTree.set("parameters/Walk/blend_position", input_vector)
	animationTree.set("parameters/Attack/blend_position", input_vector)
	animationTree.set("parameters/Roll/blend_position", input_vector)
	animationTree.set("parameters/Run/blend_position", input_vector)
	animationTree.set("parameters/Dash/blend_position", input_vector)

	if Input.is_action_pressed("run"):
		animationState.travel("Run")
		velocity = input_vector * MAX_SPEED * 1.38
	else:
		animationState.travel("Walk")
		velocity = input_vector * MAX_SPEED
else:
	animationState.travel("Idle")
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

move()

if Input.is_action_just_pressed("attack"):
	state = ATTACK

if Input.is_action_just_pressed("roll"):
	state = ROLL

if Input.is_action_just_pressed("run"):
	if input_vector != Vector2.ZERO:
		state = RUN
	else:
		state = MOVE

if Input.is_action_just_pressed("dash"):
	state = DASH

func attack_state():
velocity = Vector2.ZERO
animationState.travel("Attack")

func roll_state():
velocity = roll_vector * ROLL_SPEED
animationState.travel("Roll")
move()

func run_state():
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:
	run_vector = input_vector
	# Set animation blending based on the movement direction
	animationTree.set("parameters/Idle/blend_position", run_vector)
	animationTree.set("parameters/Walk/blend_position", run_vector)
	animationTree.set("parameters/Attack/blend_position", run_vector)
	animationTree.set("parameters/Roll/blend_position", run_vector)
	animationTree.set("parameters/Run/blend_position", run_vector)
	animationTree.set("parameters/Dash/blend_position", run_vector)

	if Input.is_action_just_released("run") or input_vector == Vector2.ZERO:
		state = MOVE

	velocity = input_vector * MAX_SPEED * 1.38
else:
	state = MOVE
	move()

func move():
velocity = move_and_slide(velocity)

func attack_animation_finished():
state = MOVE

func roll_animation_finished():
velocity = velocity * 0.2
state = MOVE

Hulda answered 3/11, 2023 at 12:27 Comment(0)
T
0

Hulda you may want to put your code all in between the 3 (~) marks on top and bottom so your code comes out cleaner.

I figured it out - you don't have move_and_slide(velocity) in your run function. This needs to occur any time the character is moving. That's why it only happens when you walk first, because the "walking" is calling the physics.

func run_state():
    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()
    move_and_slide(input_vector)
Tagmemic answered 3/11, 2023 at 22:47 Comment(0)
M
0

Hey, I changed the tags to the more appropriate ones. 👍

Maddock answered 5/11, 2023 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.