0 votes

So I have made an enemy that shoots bullets. The dead function gets called every time the bullets of type Area2D hit my player. His health is decreasing but when his health is 0 his Collision Shape isn't getting disabled. The Particles get emitted when his health is 0 which means the collision shape should get disabled and his motion should also be zero. But I can control him even when his health bar is all red. This is my dead function :

func dead(): 
    playerhp-=1
    if playerhp<=0:
        print("Player with 0 health")
        is_dead==true
        if emit == false:
            $Particles2D.emitting = true
            emit = true
        $Sprite.play("PlayerDead")
        motion=Vector2(0,0)
        $CollisionShape2D.disabled = true

And this is the script for the Bullets that enemy is firing :

extends Area2D

const SPEED =300
var velocity = Vector2()
var direction=1

func _ready():
    pass

func set_bullet_direction(dir):
    direction = dir
    if dir==-1:
        $Sprite.flip_h=true

func _physics_process(delta):
    velocity.x=SPEED*delta*direction
    translate(velocity)

func _on_VisibilityNotifier2D_screen_exited() -> void:
    queue_free()

func _on_Bullet_Advance_body_entered(body: Node) -> void:
        if "Player" in body.name:
            body.dead()
        queue_free()
in Projects by (403 points)

I can't see any mistake. Maybe something different is impeding?

Yes it's frustrating because the function is actually working since "Player with 0 health " is getting printed once my player health reaches zero but strangely enough the playerhp is going below 0 to negative values which is confusing

1 Answer

0 votes

Have you tried .kill instead of disabled?

by (75 points)

Yes that gives me an error. I just wanna know why the values are going in negative when I am giving an if statement. Very frustrating

You need to get rid of one equal sign from is_dead==true to is_dead=true

I did this and now my player's death sprite is working but he gets stuck instead of falling down. My player dies when his velocity along y axis gets greater that 1000. So it's necessary for him to fall

I don't understand. Do you want the character to fall through the map into the void?

I just can't tell you what happened bro! I set is_dead==true and and the player now dies from the bullets. But after solving this my respawn system which worked earlier is broken now as well as I want the effect that the player fell from screen after getting killed. But wherever he dies the Dead sprite stays there for a second since I am using Timer but I want the sprite to go down in the screen like he fell or something.

You should open another question for that issue

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.

Categories