0 votes

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))
in Engine by (15 points)

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!

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!

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.