Strange behavior of collision

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

Hello!

In my program there are two moving objects: player’s bullets and the monsters.
This is the script that move the monsters:

extends KinematicBody2D

var team="enemies"

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


func _process(delta):
	position.x+=4
	if position.x>(get_viewport_rect().size).x:
		queue_free()

and the script of the bullets:

extends KinematicBody2D

var itsspeed = 4
var team="bullets"

func _physics_process(delta):
	var collenemy=move_and_collide(Vector2(itsspeed,0).rotated(rotation))
	
	if position.x<1 or position.y<1 or position.x>(get_viewport_rect().size).x  or position.y>(get_viewport_rect().size).y:
		queue_free()
	elif (collenemy):
		if collenemy.collider.team=="enemies":
			collenemy.get_collider().queue_free()
			var pk=get_node("/root/Node2D/level1/pointlabel")
			pk.point+=1
			pk.text="Score: "+str(pk.point)
			queue_free()

The first problem when I coded this that when more than one monster are in the screen, the bullets shot down the first enemy but the others push away the bullets.
Besides I couldn’t get the properly name of collider with this code:

if collenemy.collider.name=="monster":

Therefore I was forced to use an own variable (“team”) to identity the collided objects. It works, but I don’t know why push away the enemies the bullets and the bullets each other otherwise?
The second problem is that as if would exist an invisible instance of the enemy monsters on the screen, although I load it from another scene. But when I shoot, the bullets often destroy at the place where the monster exists, but in another scene.
And the Score indicates hit in this case.
Has anyone encountered a similar problem in own games and can help me?

:bust_in_silhouette: Reply From: timothybrentwood
if collenemy.collider.name=="monster":

Will only work for the first instance of the monster. Godot will name subsequent instances something crazy like @@monster2@@ open the remote debugger while your game is running to see exactly what they’re named.

The best way to handle this is to let the engine handle the work for us. Add add_to_group("enemies") in the _ready() function of your enemies. Then your check becomes if collenemy in get_tree().get_nodes_in_group("enemies"):

Another way you could handle this is adding class_name Monster to the top of your enemy script then checking: if collenemy is Monster:. This is a tad more performant. I wanted to show you both ways because they both have their merits.

“The second problem is that as if would exist an invisible instance of the enemy monsters on the screen, although I load it from another scene. But when I shoot, the bullets often destroy at the place where the monster exists, but in another scene.”

The only thing I can think is the background is being drawn on top of the enemies maybe? Set the z_index property of your enemies to like 10 and see if you can at least see these enemies at that point.

Thank you, Timothy!
About the second question: I saw whether the background is being drawn on top of an enemy (I switched off its visibility), but there is no enemy node on that scene.
Is there a function, with which I can check the number of a node (the enemies in this case) possibly?

Tomi | 2021-05-16 08:00

You can use the remote debugger while the game is running to check the states of the objects in the game:
Overview of debugging tools — Godot Engine (stable) documentation in English

And a very well written article about all of Godot’s debugging features:
Getting started with debugging in Godot · GDQuest

timothybrentwood | 2021-05-16 23:32