Help with implementing distance-based damage

This problem has bugged me so many time in the past in different ways: the multiplier works fine, the only problem is that it affects my damageTaken retroactively. How can I constantly update the score, save it somewhere and keep adding whilst using the multiplier?

Hi Oliver,

Where do you want to save the score? What does the score have to do with the damageTaken and/or enemyHP variables?

I do not recognise the code, so I assume you tried to realise your own idea. If so, it would be great if you could describe your idea, your concept and the problem. Please refrain from mixing topics because it might be that I get confused.


See also:

Hi Nina,

Yes this was something I tried to implement, so that the enemy would take varying degrees of damage depending on its distance from the tower. damageTaken is the overall damage after all multipliers which is then compared with enemyHP to see if he gets killed or not.

My problem lies within the percentageDamagePerDistance multiplier:
Multiplying the enemyHitsTaken by the percentageDamagePerDistance gives me the right amount of overall damage (damageTaken) IF my character is standing still, but my problem is that as my enemy moves away from the tower, for example, the damage my enemy has already received is recalculated, being as it is bound to the percentageDamagePerDistance multiplier, which in the meantime has got smaller, therefore my damage will start to decrease.

Ok, it seems to work now, I took out the int rounding, not quite sure how the score was getting smaller but it works now! Thank you for answering!

Good job. I’m glad you made your idea work. :slight_smile:


See also:

So the solution doesn’t seem to make sense. I did have a question. The damage is calculated on particle collision, correct, do you destroy the particle after you calculate damage? Does it keep just applying damage and it shouldn’t?

I’m not sure how I would determine that, but as it’s working now, I have surmised that the score is recalculated with every particle, so my problem was that rounding was giving me lots of zeros. What I still don’t get, is how come the score was actually going DOWN at one stage.

P.S. my towers are more like sparkthrowers with a Coroutine turning them on and off at intervals (bursts of flame), so it’s not easy to see the single particle and figure out what’s going on.

Depending on @olidadda’s settings in his Particle System, the particles destroy themselves on collision.


See also:

Unity Manual: Collision module

Yeah just trying to understand the behavior. Depending on the character position could the distance be a negative number and then start reducing it.

I would agree that if it is a bunch of particles that is is being recalculated a bunch of times

That’s a good point, I don’t think it’s my case, but does Vector3.Distance always return a positive value?

Well I think yes, a vector is positive. But don’t quote me on that. It has been too many years since I took math.

The distance is always a positive value.

1 Like

I figured out what was going on… my multiplier for damage was sometimes returning negative values when the turrets were very far away, not because the distance was negative but because of my equation for damage modifier. Solved that with Mathf.clamp.

Also, I had created the damage multiplier on the enemy, but as the enemy did not have a way of detecting which tower’s distance was being multiplied by damage, it was randomly selecting, therefore multiplying the damage received by nearby towers by the distance of another one miles away, thus negative damage.

So basically I just moved the damage modifier calculation into the tower script and attached it to the targeted enemy.

Now everything’s peachy and with all the problem’s I’ve got myself into, I feel like I’m learning loads in just 2 months of learning C# :smiley:

Ben’s tutorials are amazing, if only there were one for VR in Unity!

Awesome. Great to hear. I figured something in the math would turn up. No unity vr yet, but if you are ambitious you could go down the unreal track. It has VR.

I might watch some bits of that course and see if I can derive some of it for use in Unity, but in general I feel Unity is easier, and I really don’t have time to be learning C++ as well as C# :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms