Knockback standalope error

I really think you want the knockback to be part of the if statement, there seems to be very little reason to ever be outside of the if statement. Did you try my sample?

Again I don’t know what you changed exactly so it’s really hard for me to understand why you would be getting those errors, making it impossible to help without the changed code pasted. Indent errors mean you need to add tabs to some of the lines, you probably deleted too many tabs since elif in class body means there is an elif without any indentation.

extends CharacterBody2D

@export var movement_speed = 40.0

@onready var planta =  get_tree().get_first_node_in_group("planta")
@onready var sprite = $Sprite2D
@export var knockback_recovery = 3.5
@onready var vida = 20
var knockback = Vector2

func _physics_process(_delta):
	if is_instance_valid(planta):
		var direction = global_position.direction_to(planta.global_position)
		velocity = direction*movement_speed
		move_and_slide()
		
		knockback = knockback.move_toward(Vector2.ZERO, knockback_recovery * _delta)
		velocity += knockback



		if direction.x > 0:
			sprite.flip_h = false
		
		elif direction.x < 0:
			sprite.flip_h = true
			
	else:
		planta = get_tree().get_first_node_in_group("planta")
		move_and_slide()
	






func _ready():
	$lil.play("move")








func _on_area_2d_area_entered(_area):
	if _area.is_in_group("planta_area"):
		$lil.play("attack")
	elif _area.is_in_group("bala"):
		knockback = Vector2.ONE * 100 
		print("Knocked back! ouch!")
		var diff: float = self.global_position.x - _area.global_position.x
		knockback.x *= sign(diff.x)
		vida -= 9
	if vida <= 0:
		queue_free()


func _on_area_2d_area_exited(_area):
	$lil.play("move")

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.