0 votes

Hi all.

I have a few Area2Ds. One is called BaseArea, and is the only one that doesn't move. LazerShot and Enemy are the moving Area2Ds. What I was wondering is if there is a way to have Enemy emit a signal if it only collides with LazerShot.

extends Area2D

signal kill
export (int) var enemy_speed


func _ready():
    set_process(true)


func _process(delta):
    position -= Vector2(enemy_speed * delta, 0)



func _on_VisibilityNotifier2D_screen_exited():
    if position.x < 0:
        print("Deleting…")
        queue_free()


func _on_Enemy_area_shape_exited(area_id, area, area_shape, self_shape):
    queue_free()

The above code is from Enemy.gd.

in Engine by (197 points)

1 Answer

0 votes

Hi, you can use body_entered(body). In body you can use the parameter to check if the body who entered the area is lasershot. For example, you can define a function in lasershot that does nothing, but you can use to identify a lasershot:

#this goes in lasershot
func lasershot():
    pass

Then in your enemy, you check if body has that method:

#don't forget to connect the body_entered signal with this function
func on_body_entered(body):
     if body.has_method("lasershot"):
        #here you put whatever you wanna do when laser shots hits enemy.

Perhaps there is a better way, but i usually do this. You can also set collission masks/layers to set enemy only can be collided by lasershot. For example, you put lasershot in layer 2, and you only enable second bit on mask of enemy, or something like that.

by (3,388 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.