_on_area_3d_body_entered signal acts weird with boolean

Godot Version

4.2.1.stable

Question

So basically I’m trying to make a Meshinstance3D change color if I stand on it (which already works if I click my left mouse button).
So I made a variable “active” which I turn on if I enter an area3d and I turn it of if I exit it.
Seems very simple and logical to me, but if I stand in the area3d, the boolean just keeps switching on and off. I can’t find anything about it online, so all help would be much appreciated.
(Please, view the attached and screenshots)

Screenshot of code:

print output:

I’m not quite sure what is causing the problem but it is probably worth checking if there aren’t any other bodies interfering with your area detection, like the floor for example. Can you change the code to the following so we can see who is triggering the signals?

func _on_area_3d_body_exited(body):
	active = false
	print("Exiting:" + body.name)

func _on_area_3d_body_entered(body):
	active = true
	print("Entering:" + body.name)

If there are bodies that aren’t the player popping up in the output, then you can prevent them from interfering by adding a check in those two functions. Note that you have to add class_name Player inside of your player script for this to work.

func _on_area_3d_body_exited(body):
	if !body is Player:
		return
	active = false

func _on_area_3d_body_entered(body):
	if !body is Player:
		return
	active = true

Alternatively you could also change the collision mask of your portal’s area to only detect the player in the first place; or if you later want to change it so other objects like NPCs or Rigidbodies can use the portal as well.

Hope this helps. :>

1 Like

Hey there,
First off all thank you for trying to solve this problem with me.

I’ve changed the Collision layers and masks, but to no avail.
The function seems to work fine with print statements, but for some reason, the boolean only freaks out inside of the _on_area_3d_body_entered function (since it works perfectly fine outside of that)

screenshot of the functions working as intended:

Sadly enough, it’s probably a bug. I’ve tested the same code in another project of mine and there it works perfectly.
Thank you for your time and effort though.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.