How to detect when Body is not in Area when input pressed?

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

Hi all,

I’m working on a 2D game where the player (KinematicBody2D) must push an input/action within an Area2D to pass the level. However, I’m struggling to figure out how to make it so that if the player pushes the input before entering the Area2D, they fail the level.

I’ve tried making a function that if player.isnt_moving() they lose, but no dice.

I’ve also tried to do something like if !player._on_FinishArea_body_entered()

I’ve also tried adding a new Area2D with a Collision2D that covers the whole level. From there, I added a new _on_body_entered() with if player.isnt_moving()

Any help would be appreciated

:bust_in_silhouette: Reply From: conbor_

Seeing as I can’t see the code right now (next time try to include it so we can know 100% what is happening ;)) I will do my best to try and give a good response for you!

I would start by adding an Area2D to the player which can only detect your “level end” Area2D using collision layers. With this setup, you can use code along the lines of this on the player’s Area2D:

extends Area2D
func _process(delta: float) -> void:
       var overlap = get_overlapping_areas() 
       #returns a list of overlapping areas

       if overlap == null:
   = false

   = true

       #the owner tag can reference variables and functions that 
       #exist in the root node of a scene. Granted your player is a 
       #seperate scene being instanced in your levels, you can add a 
       #win variable to the player script as a boolean statement.

now with this logic in place, on the player script its as easy as something like this:

extends KinematicBody2D

var win = false

func _process(delta: float) -> void:

       if Input.is_action_pressed("action") && win == true:
       elif Input.is_action_pressed("action") && win == false:

where win_game() and lose_game() can either be functions you define, or the logic required for winning and losing :slight_smile:

This stuff can also be done using signals, but seeing as you already tried that, I thought I would offer a bit of a different approach! Make sure when you do this that the Area2D logic is on an Area2D that is a child to the root player node, and that it’s collision mask only lets it detect your level end Area2D!

Best of luck!

This helped a lot, thank you very much!

PyWhacket27 | 2023-02-17 03:27