enemy sprite goes up when player enters it's Area2D

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

hello
i’ve set up my code but as the title suggests when a player enters the Area2D collision zone it just goes crazy defying the gravity rules i defines

here’s the code:

 
extends KinematicBody2D
#gravity logic and variables, getting the hang of it
onready var player = get_parent().get_node("player")
export var minSpeed = 150
export var maxSpeed = 250
export (int) var gravity = 1000
var velocity = Vector2.ZERO
var detected = null
onready var animation = $AnimatedSprite
var playerpos = Vector2.ZERO


#player detection logic, ideally if it works
func _on_Area2D_body_entered(body):
	if player == body:
		detected = true


#function for animations and player detection logic
func animations_and_detection():
	velocity = Vector2.ZERO
	if detected:
		playerpos = player.global_position
		velocity = position.direction_to(playerpos).normalized() * minSpeed 
	else:
		animation.play("idle")
	

func _physics_process(delta):
	animations_and_detection()
	velocity.y += gravity * delta
	velocity = move_and_slide(velocity, Vector2(0, -1))

Before we get too deep into searching for issues, is it possible you’ve marked the space override property in the Area2D options? Area2D are made to have the option of defying physics so this could be quite normal behavior!

maxwellc055 | 2021-05-27 04:22

hey uh I actually solved the issue by removing the + from the += in my gravity logic

velocity.y += gravity * delta

i’m still not certain what was causing it i’ll be honest but i just knew it was gravity related so i tinkered the gravity logic a bit and it got fixed, thanks for the help tho!

antigo | 2021-05-27 04:25