Collision Animation

gsweegswee Posts: 3Member
edited June 18 in 2D

Hello, I want my fireball upon collision to play an impact animation before queue_free(). I've tried to stop the fireball by changing velocity but it doesn't work. CollisionShape2D does disappear but the rest don't work.

I'm following the tutorial here:

Code is uploaded.

Animation to implement here: (https://i.imgur.com/V0ZFyee.png)


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin

    Welcome to the forums @gswee!

    Looking at the code, I think there are two issues going on. The first is that the animation in _physics_process is overriding the collision animation before it has a chance to play. The second is because you are setting the velocity on the x axis in physics process, setting it to zero in _on_Fireball_body_entered won't stop it from moving, as it will be instantly overridden in the _physics_process function.

    However, you should be able to fix both issues by changing _physics_proces, _on_Fireball_body_entered and on_timeout_complete to the following (untested):

    # We need a variable to tell if we have collided or not:
    var collided = false
    func _physics_process(delta):
        # if we have NOT collided, then move and play the default animation
        if (collided == false):
            velocity.x = SPEED * delta * direction
            translate(velocity)
            $AnimatedSprite.play("default")
    
    func _on_Fireball_body_entered(body: PhysicsBody2D) -> void:
        # check if collided is already equal to true, so we do not reset the timer every
        # time something collides with the fireball
        if (collided == true):
            return
    
        $CollisionShape2D.disabled = true
    
        # set collided to true, so the fireball stops moving
        collided = true     
    
    #   # CONNECT TIMER 
        timer.connect("timeout",self,"on_timeout_complete")
        add_child(timer)
    
        $AnimatedSprite.play("collide")
    
    func on_timeout_complete():
        print("bullet stopped colliding")   # test
        queue_free()
    
  • gsweegswee Posts: 3Member

    Thank you! The animation worked.

    One problem is that the sprite doesn't play the collide animation when the fireball is shot within a wall.

    Also, on_timeout_complete() isn't connected, so queue_free() doesn't happen.


    (yellow = collision disabled)

    I plan to investigate these issues whenever possible. If you can help that would be fantastic. Is the fireball queue_free() before it can play the animation?

    I'm still getting used to how Godot runs code, as I come from traditional coding background.

  • TwistedTwiglegTwistedTwigleg Posts: 2,655Admin

    @gswee said:
    Thank you! The animation worked.

    Awesome! Happy to help :smile:

    One problem is that the sprite doesn't play the collide animation when the fireball is shot within a wall.
    ... Is the fireball queue_free() before it can play the animation?

    Hmm, good question. I'm not sure what is going on, to be honest. Maybe try removing the code that checks if there is already a collision and see if that fixes it? Perhaps the collision happens so fast that it returns before it can set the animation or something.

    Also, on_timeout_complete() isn't connected, so queue_free() doesn't happen.

    You need to start the timer. Add timer.start() after add_child(timer) and it should work.

  • gsweegswee Posts: 3Member
    edited July 4

    You need to start the timer. Add timer.start() after add_child(timer) and it should work.

    Oh right, forgot about that. Thanks! The fireball queues free on collision now.

Leave a Comment

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