how do i get moving enemys to have projectile attacks that hit player

riphleriphle Posts: 10Member
edited January 14 in 2D

i am creating a 2d platformer and would like help / ideas on how to get my 2d enemys to attack the player with a prjectile attack ( throwing fireball/ spear ect). i already have the enemys made so they kill the player when they run in to them. what is a good way to add a projectile attack to my enemys? any and all help would be most appreciated>

Tags :


  • SIsilicon28SIsilicon28 Posts: 748Moderator
    edited January 14

    Well, what I do when I want to have projectiles in my game is-
    1. make the projectiles their own entity/scene.
    2. have the shooter spawn the projectile with initial variables
    3. give the projectile code to affect other entities in certain ways.

    For example, I have a game with a laser cannon in it that the player can collect.
    While it's active, the laser cannon creates an instance of the laser. it is given a position among other variable before it is added to the scene. However, to make sure that the laser doesn't move with the player, it is added as a child of the world. In this case it was the player's parent.
    The lasers themselves have a script that causes to move across the scene.
    They also have an Area2D that is set up so that whenever another body enters it, in this case an asteroid, it destroys it. The code for destroying the asteroid is in the asteroid itself, but can also be in the laser. It depends on how you want to structure your game logic.

    This sort of design works best for ballistic projectiles, such as arrows, fireballs, and cannon balls. If you want a projectile that's more instant and fast moving, like a bullet, then you're better off with a simple raycast.

  • riphleriphle Posts: 10Member

    Sisilcon28 I thank you greatly. I was able through some experimentation And a little research able to get my enemy's to throw a projectile at the player when the player come close. What I ended up doing was instance the object at a position2d when the the player walks in to collision2d circle. I thank the coding here was what was screwing me. It all all seems so obvious now that I got it working thank you again

Leave a Comment

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