Can't get Rigidbody2d and Area2d (now another rigidbody) collision to work?

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

Hi, I just started using godot (so excuse me if I’m making dumb mistakes), essentially, I have a “ball” in my game that is a rigidbody2d, and I want to detect when it collides with a area2d, so far I have this code for the detection part (the code is attached to the ball node):

func _on_Area2D_body_entered(body):
if body.get_name() == "BALL":
	inwaterorno = true
pass # Replace with function body.

(I have no idea whats wrong with the indentation here but it is correct in godot)

I want to use the variable “inwaterorno” to see if the ball is in the area or not, but when I print it using print("inwaterorno")', it prints this whenever the ball enters: True False True False True False True False True False True False True False True False True False True
(it goes on but you get the point)
Why is the variable alternating?, is there a fix to this or perhaps another way i can detect for a collision between a area2d and a rigidbody2d? I’ve searched the web and i’m still stumbled on what is happening.


Edit: So I changed my method up (so now its detecting for a collision between a rigidbody and another rigidbody) and it still seems to show the same problem:
This is the entire code for the ball:

extends RigidBody2D

# Declare member variables here. Examples:
# var a = 2
var speed = 0
var hit = false
var max1 = false
var constspeed = 0
var inwaterorno = false
# var b = "text"

var mouse_position = get_global_mouse_position()
# Called when the node enters the scene tree for the first time.
func _ready():
	contact_monitor = true
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_pressed("ui_down"):
		hit = true
		hit = false
func _physics_process(state):

	if Input.is_action_pressed("mouseleft"):
		gravity_scale = 4
		Engine.time_scale -= 0.01
		if Engine.time_scale < 0.1:
			Engine.time_scale =0.1
		if FuelBar.runout == false:
			speed += 75

		if speed > 12000:
			speed = 12000

	if Input.is_action_just_released("mouseleft"):
		gravity_scale = 1
		apply_central_impulse(Vector2(get_global_mouse_position() - position).normalized()*speed)
		speed = 0
		Engine.time_scale = 1
	var bodies = get_colliding_bodies()
	for body in bodies:
		if body.get_name() == "JUMPYTHING2":
			inwaterorno = true
			inwaterorno = false

the indentation finally works so YAY, however, the collision unfortunately still doesn’t work:
instead of using func _on_Area2d_body_entered(body):, i am now using get_colliding bodies, however when i use print out the variable and the bodies, it shows this when colliding:


it goes on obviously, it seems as though it is detecting the body for one loop and not detecting it for another, another thing i’ve noticed is that this only happens on the ball, and not some of the other rigidbodies i have in my game. (OH, and note that the “1” is there so i can see when the code loops)

Where are you printing the variable value from (you don’t show that in your code above)? Also, where else are you setting the value of that variable? Without all of the code that relates to your question, it’s hard to provide any useful information.

Also, not sure what happened to your indents either, but you can simply fix them manually by editing your message. Just add 4 spaces for each level of indention.

Typically, you should be able to paste working code into a message, select it , and then press the { } button in the forum editor’s toolbar to get it formatted properly here.

jgodfrey | 2020-12-01 21:29

I’m printing it from the func _physics_process(state): (although I did also try to print it in func _process(delta):, which give the same result), and I’m setting the variable in func _on_Area2d_body_entered(body): with inwaterorno = true, I will also try to get the entire code on. Thanks!

godotnoob111 | 2020-12-02 02:13