What is an efficient way to have my bullets explode on impact?

Mi1kMi1k Posts: 2Member
in 2D

I am working on the shooting and projectiles mechanism within my game and want to have the bullets explode upon impact.
As of the moment I do seem to have the bullets "exploding" and hitting multiple targets at once, however through examination on the debug screen, it seems that all of the bullets have their explosion area collision shape changed whenever any one makes impact with a body. I am not sure why this is.
I wanted to see if there is a more effective method to have the bullets explode only on impact.

    extends Area2D
    export (int) var speed
    export (int) var damage
    export (float) var lifetime
    var explosionRadius
    var velocity = Vector2()

    func start(_position, _direction):
        position = _position
        rotation = _direction.angle()
        $Timer.wait_time = lifetime
        velocity = _direction *speed
        $Timer.start()

    func _process(delta):
        position += velocity * delta

    func _ready():
        $Explosion/CollisionShape2D.shape.radius = 0

    func explode():
        $Explosion/CollisionShape2D.shape.radius = explosionRadius
        queue_free()

    func _on_Bullet_body_entered(body):
        if body.has_method("takeDmg") and body.alive == true:
            body.takeDmg(damage)
        explode()

    func _on_Timer_timeout():
        explode()

    func _on_Bullet_area_entered(_area):
        queue_free()

    func _on_Explosion_body_entered(body):
        $Particles2D.emitting = true
        if body.has_method("takeDmg") and body.alive == true:
            body.takeDmg(damage)

    func _on_Explosion_body_exited(body):
        $Explosion/CollisionShape2D.shape.radius = 0

This is the current code for my setup. It sets the explosion areas shapes to 0 at the start and increases the size of the radius when the bullet makes impact , it then checks if bodies within the area can take damage and ~~~~if they can then it calls the take damage function on that body.

Best Answer

  • vinpogovinpogo Posts: 36
    Accepted Answer

    you have to make sure the collisionshape is not shared across instanced scenes. Inside the inspector you should find a tickmark saying Make Local. If you check that, changing the radius of one instanced scene shouldnt effect others.

Answers

  • vinpogovinpogo Posts: 36Member
    Accepted Answer

    you have to make sure the collisionshape is not shared across instanced scenes. Inside the inspector you should find a tickmark saying Make Local. If you check that, changing the radius of one instanced scene shouldnt effect others.

  • vinpogovinpogo Posts: 36Member

    Regarding efficiency, i dont know if its more efficient, but i would have done it this way:
    I wouldnt alter the radius of your collisionShape, but rather set the collision mask to not mask an layer. Then at the point where you set the radius, i would simply set the correct collisionMask again.

    As i said, i dont know wether this is more efficient....

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file