Search Unity

What is a better way to handle damage from a projectile weapons?

Discussion in 'Game Design' started by mark23537, Mar 13, 2021.

  1. mark23537

    mark23537

    Joined:
    Dec 12, 2020
    Posts:
    3
    Hi, so I'm trying to make a top down shooter game. I'm using a physics to move all my projectiles but I'm not sure what is the best way to handle damage.

    option #1:
    WeaponGameObject contains weapon script with scriptable object to handle weapon stats (damage, fireRate and etc) and spawning the projectile.
    ProjectileGameObject will have projectile script that handle physics and collision and applying the damage.

    option #2:
    WeaponGameObject still contains weapon script with scriptable object to handle weapon stats but the damage will be move to ProjectileGameObject

    My problem with option #1 is I need to get the projectile script to change the damage value every time I fire the weapon but the option is easier to have more weapon variation and it will be easier to change the value because I only need to change it in one place. And on the option 2 the problem is I need to have multiple ProjectileGameObject to handle different damage and changing the value is hard. So what option is better or is there a better way to handle this. Thanks and sorry for my bad english
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Unless you got specific mechanics, your projectiles should contain anything that is neccessery 5o deal damage. You may need pass a velocity, position and direction from the gun, at the time of firing. Unless having some fancy mechanics, another value your projectile may want to have, is the player who fired it.

    Rest things happens on the bullet side, until it hits the target, or obstruction. Using information about who fired projectile, you will know, if you hit an enemy, or friendly, or even self.

    Other than that, your gun should not be concerned about that.
     
    SparrowGS and Billy4184 like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I would put it all on the projectile. It's not that it's more "correct" or anything like that, it's because that's what'll give me the most flexibility later.

    Want to allow a projectile to bounce off a shield and do half damage? Set it on fire and add extra damage? Reduce or reset its lifetime? Whatever it is, you want all of the related variables to be accessible in one place.
     
    SparrowGS and Billy4184 like this.
  4. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    I agree with both posts above, mainly because it is efficient. Code should be kept as close as possible to the place it is used. Unless your weapon is a catapult or something, then the damage should be determined by the bullet.

    Don't make too many assumptions about efficiency. GetComponent is not incredibly inefficient. Always test before discarding the architecture that is most intuitive to you.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I'm also with the opinions posted above me.

    but why do you think that about a catapult? I would argue the logic goes on the gun in cases like insta-hit lasers, a catapult is just like every other projectile weapon, a lot can happen to it in flight.
     
    Last edited: Mar 17, 2021
    Joe-Censored likes this.
  6. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    Oh yeah I meant it would be affected by the weapon in terms of speed, but yeah logic is same.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    The same applies to most projectile weapons, though. A bow will determine the energy given to an arrow. The barrel of a gun will effect how much energy a projectile has when it exits the muzzle. And so on. That's all stuff that can be applied as initial conditions when the projectile begins its flight.

    It's efficient for us humans. For computers it's the opposite. But that's not something anyone should care about until they're fairly advanced in their programming and have identified a performance issue.
     
  8. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    6,025
    That's the point ;)
     
    BrandyStarbrite likes this.
  9. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    Ditch the physics. It’s unnecessarily expensive for bullets.

    instead make a game object specially for projectiles that have a movement speed, then do a ray cast between fixed updates from one point to another.

    And use a pool to avoid hitting the garbage collector.
     
  10. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    Definitely.