0 votes


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?

in Engine by (150 points)

2 Answers

+3 votes
Best answer

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():

func _fixed_process(delta):

    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.

by (5,248 points)
selected by

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


You're welcome. ^_^

0 votes

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 ):
    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

by (226 points)

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

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 Frequently asked questions and 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 [email protected] with your username.