How to check if my player is inside an object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ismaelgame7
:warning: Old Version Published before Godot 3 was released.

Hello,

I wanted to know, How to check if my player is inside an object?
I already searched in videos, poster here but I did not find anything.
I can only check if it has entered an object but not if it is inside.

Can someone help me please?

:bust_in_silhouette: Reply From: avencherus

I have to assume you’re talking about areas and their enter and exit signals.

Areas have a method called overlaps_body(body)

You can check for that, though on the first frame of the game running, it won’t detect anything. Assuming you have an area setup and a body to reference: Here’s a simple example.

extends Area2D

onready var body = get_parent().get_node("KinematicBody2D")

func _ready():
	set_fixed_process(true)

func _fixed_process(delta):

	print(overlaps_body(body))
	if(overlaps_body(body)): set_fixed_process(false)

Another method is tracking state with a flag. Though it may not lend itself to a very scalable design.

var is_inside_object = false

When an object enters, flag it as inside, when it exits, flag it false.

As long as it has not signal as exited, it should be inside.

Thank you so much,
I made a project for anyone who wants to use. :slight_smile:

Dropbox - CollisionArea.zip - Simplify your life

ismaelgame7 | 2017-04-01 20:35

You’re welcome. :slight_smile:

avencherus | 2017-04-01 20:48

:bust_in_silhouette: Reply From: Serenade

Well, i had a similar need, wanted to make a bullet, which, when hits the body(enters its area), it subtracts HP and queue_free it self
i did that by first referencing script

var danger = preload("res://bullet_scene.gd")

(which is a script for bullets and slashes and such…)

then you add a Area2D script to first object, add a 2D collision shape IN that Area2D same for 2nd object

and add signal to the script, like …

func_on_www_area_enter( area ):
    damage()
    print("entered area!")

I found that for bullets at least, you shouldnt use kinematicbody nor physics one(with separate collision node as well), cuz then it wont work well with detection when speed get high

This only check if the player entered, not if is inside or not.
Anyway thanks for answering me :smiley:

ismaelgame7 | 2017-04-01 20:42