My variable (axis) = null, but it worked fine before I added knockback. HELP!

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

extends KinematicBody2D

var MAX_SPEED = 350
var ACCELERATION = 2000
var motion = Vector2.ZERO
var roll_vector = Vector2.LEFT
onready var swordHitbox = $Position2D/Hitbox2

func _ready():
swordHitbox.knockback_vector = roll_vector

func move_state(delta):
var axis = Vector2.ZERO
axis.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
axis.y = Input.get_action_strength(“ui_down”) - Input.get_action_strength(“ui_up”)
axis = axis.normalized()

if axis != Vector2.ZERO:
	roll_vector = axis
	swordHitbox.knockback_vector = axis

func _physics_process(delta):
motion = move_and_slide(motion)
var axis = move_state(delta)
if axis == Vector2.ZERO:
apply_friction(ACCELERATION * delta)
else:
apply_movement(axis * ACCELERATION * delta)
if Input.is_action_just_pressed(“Attack_Down”):
$Fight.play(“Attack_Down”)
elif Input.is_action_just_pressed(“Attack_Up”):
$Fight.play(“Attack_Up”)
elif Input.is_action_just_pressed(“Attack_Left”):
$Fight.play(“Attack_Left”)
elif Input.is_action_just_pressed(“Attack_Right”):
$Fight.play(“Attack_Right”)

	
	

func apply_friction(amount):
if motion.length() > amount:
motion -= motion.normalized() * amount
else:
motion = Vector2.ZERO

func apply_movement(acceleration):
motion += acceleration
motion = motion.clamped(MAX_SPEED)

:bust_in_silhouette: Reply From: grilix

I looks like you are not returning the value on move_state. Please try adding return axis at the end of that function.

I’d suggest always adding the return type to the functions. This way the IDE can help you detect these kind of things: func move_state(delta) -> Vector2:

THANK YOU!

This is my first project and I am so glad that I got help within a day. You are amazing!

Have a good day.

Dragonwarrior2004 | 2020-05-18 16:38