I tries searching for an answer but couldn't find one?
How to I check if the collising object is in a particular group in kinematic body 2d??
Here are a couple ways I've checked to see if a collision object is a certain node type:
# Method 1: using "is" to check if the node is a KinematicBody2D node
# (Sometimes can cause Godot to believe there are circular references when
# comparing against custom classes. Not sure why this happens, but Godot may
# refuse to run the script, so you may need to use Method 2 for custom classes)
if (collision_object is KinematicBody2D):
pass
# Method 2: using "has_method" to check if the node is a KinematicBody2D node
# (If another node has the same function, it will be be mistakenly considered
# a KinematicBody2D node through this method)
if (collision_object.has_method("move_and_slide"):
pass
There might be other ways to check, but those are the ones I have most commonly used. :smile:
- Edited
Hi Anderoo99
I found the easiest way is add an Area2D added to one or of the objects. On the object use the signal _on_Area2D_body_entered
Then in the code on that object
func _on_Area2D_body_entered(body):
if body.is_in_group("enemy"):
print(body.name)
print(body.position)
Make sure you add the name to the node node group.
Not sure if this is what you need? also if you add Area2d's to both object you can use the below on both objects(change the node group on each)
Enemy code
func _on_Area2D_area_entered(area):
if body.is_in_group("player"):
print(area.name)
Player code
func _on_Area2D_area_entered(area):
if body.is_in_group("enemy"):
print(area.name)
Hope this helps.
- Edited
@Sparrow said: Hi Anderoo99
I found the easiest way is add an Area2D added to one or of the objects. On the object use the signal _on_Area2D_body_entered
Then in the code on that object
func _on_Area2D_body_entered(body): if body.is_in_group("enemy"): print(body.name) print(body.position)
Make sure you add the name to the node node group.
Not sure if this is what you need? also if you add Area2d's to both object you can use the below on both objects(change the node group on each)
Enemy code
func _on_Area2D_area_entered(area): if body.is_in_group("player"): print(area.name)
Player code
func _on_Area2D_area_entered(area): if body.is_in_group("enemy"): print(area.name)
Hope this helps.
Hey Thanks for the reply, I was able to figure it out so i didn't check back, sorry for that :-) I like it how my questions on godot get answered everytime, even if they are stupid, I got downvoted in unitys forum for asking stupid questions. Thanks!!!
@TwistedTwigleg said: Here are a couple ways I've checked to see if a collision object is a certain node type:
# Method 1: using "is" to check if the node is a KinematicBody2D node # (Sometimes can cause Godot to believe there are circular references when # comparing against custom classes. Not sure why this happens, but Godot may # refuse to run the script, so you may need to use Method 2 for custom classes) if (collision_object is KinematicBody2D): pass # Method 2: using "has_method" to check if the node is a KinematicBody2D node # (If another node has the same function, it will be be mistakenly considered # a KinematicBody2D node through this method) if (collision_object.has_method("move_and_slide"): pass
There might be other ways to check, but those are the ones I have most commonly used. :smile:
Hey, thanks for answering, I ended up using Area2d :-)