Rigidbody2D collides but doesn't trigger logic?
Asked Answered
O

3

0

Hello!
I have an asteroid spawner that spawns RigidBody2D asteroids of varying sizes and when I shoot RigidBody2D projectiles at it they collide and interact in-game as you'd expect but when I try to trigger logic there is no output. This script is attached to each asteroid that spawns and have also tried the syntax "body_entered.connect(_on_body_entered)" -> Side question: what's the difference?

func _ready() -> void:
        contact_monitor = true

       #are both syntax correct and function the same?
       #body_entered.connect(_on_body_entered)

	connect("body_entered", _on_body_entered)

func _on_body_entered(body) -> void:
	if body.is_in_group("laserProjectile"):
		print("collided with laser projectile")
	elif body.is_in_group("rocketProjectile"):
		print("collided with rocket projectile")
	elif body.is_in_group("minigunProjectile"):
		print("collided with minigun projectile")
Obstruent answered 21/12, 2023 at 23:45 Comment(0)
V
0

In case the group tests are failing, add a print at the beginning of _on_body_entered().

Obstruent Side question: what's the difference?

The first connect(), which uses the Signal class, is preferable to the second, which uses the Object class. The reason is that a mistake in the signal name will be detected earlier.

There's more explanation here:
https://docs.godotengine.org/en/4.2/classes/class_object.html#class-object-method-connect

Vesper answered 22/12, 2023 at 3:2 Comment(0)
O
0

Vesper The first connect(), which uses the Signal class, is preferable to the second, which uses the Object class. The reason is that a mistake in the signal name will be detected earlier.

Vesper Thank you that is good to know

Also I added a print("collision") above the tests but nothing.

Below is the node hierarchy for one of my projectiles and asteroids just as references and screenshot of projectile group name.



Obstruent answered 22/12, 2023 at 15:51 Comment(0)
O
1

I see, so I did not set max contacts reported for the asteroids, which defaults to 0. So even if contact monitoring is true, no collisions are actually reported.

For anyone in the future here's a screenshot of where to set these in the inspector but make sure you set:

  • contact_monitoring = true (check the box)
  • max_contacts_reported = (int > 0)
Obstruent answered 22/12, 2023 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.