0 votes

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?

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)
http://docs.godotengine.org/en/stable/classes/class_area2d.html?highlight=area2d#class-area2d-overlaps-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.

by (5,232 points)
selected by

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

https://www.dropbox.com/s/1sukgc160pxa3t8/CollisionArea.zip?dl=0

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

by (225 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 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.