Character not doing stuff.. as it should.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WaterDev

Basically, This problem have been really, really confusing and frustrating.
So I made a code so my character uses animations when using different keys on the keyboard.
BUUUUT it somehow made my character even faster and im like ???
Here’s my code for the character:

extends KinematicBody2D
#Variables and stuff

const Friction = 500

var Velocity = Vector2.ZERO

const MAX_SPEED = 80

onready var animationPlayer = $AnimationPlayer

enum{
	MOVE,
	ROLL,
	ATTACK
}

var State = MOVE

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

func _ready():
	animationTree.active = true

func _physics_process(delta):
	match State:
		
		ROLL:
			pass
		
		ATTACK:
			
			attack_state(delta)
		
		MOVE:
			move_state(delta)
	move_state(delta)
	
	
	


func move_state(delta):
		#Movement Inputs
	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()
	
	#Thing that actually do the Movement
	
	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, Friction * delta)
	else:
		animationState.travel("Idle")
		#Complicated Meeeeeethsss
		
		Velocity = Velocity.move_toward(Vector2.ZERO, Friction * delta)
	Velocity = move_and_slide(Velocity)
	if Input.is_action_pressed("attack"):
		State = ATTACK
	pass
func attack_state(delta):
	animationState.travel("Attack")
	pass

Any help would be greatly appreciated. Thanks in advance.

:bust_in_silhouette: Reply From: jgodfrey

Without a detailed look at the posted code, I notice that you’re likely calling move_state(delta) twice from _physics_process - once from inside the match when the state is MOVE and once at the bottom of the function.

I assume that double-call is leading your statement of made my character even faster (??)

Omfg how did I not realize this…
Thank you good sir, Was stuck on this for awhile lol
Have an absolutely fantastic day!

WaterDev | 2020-09-22 18:22