0 votes

Hello guys I have a problem with my game. I make a space shooter and i made a flash shader when the enemies get hit. The problems came when i try the game and every instance of my enemies start the shader when one get hit. I will leave the code below:

Enemy

    extends Area2D

export var health = 100
export var speed = 50
export var fire_rate = 2
export var points  = 10
var fire_rate_timer = Timer.new()
var fly_can_shoot = true
var hit_animation = preload("res://Hit_animation.tscn")

func _ready():
    $Skin.play()
#set the timer for the fire rate
    set_timer()
    EventBus.connect("hit_animation",self,"on_hit_animation")

func _process(delta):
#moving by itself to the left side of the screen
    #global_position.x -= speed * delta
#elimate if it's out of the screen
    if global_position.x < -10:
        queue_free()
#shoot
    if fly_can_shoot == true :
        Methods.play_sound("Shoot")
        create_fly_projectile(Global.Fly_projectile,global_position)
        fly_can_shoot = false
        fire_rate_timer.start()
        print("shoot")

func _on_Fly_area_entered(area):
        health -= 1
        check_health()

        if area.health >= 0:
            EventBus.emit_signal("hit_animation")
            EventBus.emit_signal("hit")

func on_shoot_timeout():
    fly_can_shoot =true

func set_timer():
    fire_rate_timer.set_one_shot(true)
    fire_rate_timer.set_wait_time(fire_rate)
    fire_rate_timer.connect("timeout",self,"on_shoot_timeout")
    add_child(fire_rate_timer)

func check_health(): 
    #check if enemy he still has health
    if health <= 0:
        Methods.play_sound("Explosion")
        EventBus.emit_signal("create_explosion",Global.Explosion,global_position)
        Global.score += points
        EventBus.emit_signal("score")
        queue_free()  
    else:
        Methods.play_sound("Hit")

func create_fly_projectile(projectile,location):
    var projectile_instance = projectile.instance()
    get_parent().add_child(projectile_instance)
    projectile_instance.global_position = location + Vector2(-10,0)
    print("made")

func on_hit_animation():
    var hit_animation_instance = hit_animation.instance()
    get_node("Skin").add_child(hit_animation_instance)
    hit_animation_instance.play("Flash")

Animation player for the shader

extends AnimationPlayer

var flash_duration_timer = Timer.new()

var flash_duration = 1.0

func _ready():
    Global.Hit_animation = self
    set_timer()
    EventBus.connect("hit",self,"on_hit_start")

func _exit_tree():
    Global.Hit_animation = null

func set_timer():
    flash_duration_timer.set_one_shot(true)
    flash_duration_timer.set_wait_time(flash_duration)
    flash_duration_timer.connect("timeout",self,"on_flash_timeout")
    add_child(flash_duration_timer)

func on_hit_start():
    flash_duration_timer.start()

func on_flash_timeout():
    self.stop(true)

I tried finding this on forums but nothing works. It is a way to make the instances independent from each other? for the part when it need to dies work perfectly and i'm confuse why this don't. Thanks and sorry for my english.

Godot version 3.3.2 stable
in Engine by (47 points)

1 Answer

+1 vote
Best answer

On the enemy Mesh / Sprite > Options (click on the model / texture thumbnail), and try setting the Resource > Local To Scene:

enter image description here
If that doesn't work, try follow this person's solution and see if it works for you too.

Hope it helps!

by (1,010 points)
edited by

Than you soo much it works!

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 Frequently asked questions and 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 [email protected] with your username.