Maths to calculate enemy damage based on distance from player
Barrythe
Member Posts: 82
This is making my head hurt - I have a stationary player on the left of the screen, and enemies spawning on the right. The enemies walk towards the player, and the player can fire at the enemies. As a test I gave the enemies 100 health and each bullet removes a fixed amount of health per hit, until health reaches zero and the enemy dies.
Now I am trying to calculate the damage so that my weapon acts like a shotgun - the closer the enemy is the more damage dealt. I have set a rule on the enemy so that when the bullet hits, damage is calculated from a magnitude between the player and the enemy. The trouble is that this setup means that the larger the distance between the player and the enemy the larger the damage, whereas I need the opposite, so that the shorter the distance the greater the damage. My current rule attached to the enemy says that when the enemy collides with a bullet: (by the way "game.PlayerPos "is a global attribute constrained to the players x position).
self.Health = self.Health - self.Health * magnitude(self.position.X - game.PlayerPos , self.Position.Y) / 1000
This generally gives me a nice small number between 20 and 40ish, but is there a way I can achieve the inverse so that the number gets larger as the distance gets shorter? I'm no maths genius so be gentle with me if my equation is a load of rubbish :-)
Now I am trying to calculate the damage so that my weapon acts like a shotgun - the closer the enemy is the more damage dealt. I have set a rule on the enemy so that when the bullet hits, damage is calculated from a magnitude between the player and the enemy. The trouble is that this setup means that the larger the distance between the player and the enemy the larger the damage, whereas I need the opposite, so that the shorter the distance the greater the damage. My current rule attached to the enemy says that when the enemy collides with a bullet: (by the way "game.PlayerPos "is a global attribute constrained to the players x position).
self.Health = self.Health - self.Health * magnitude(self.position.X - game.PlayerPos , self.Position.Y) / 1000
This generally gives me a nice small number between 20 and 40ish, but is there a way I can achieve the inverse so that the number gets larger as the distance gets shorter? I'm no maths genius so be gentle with me if my equation is a load of rubbish :-)
Comments
My games force me to use parts of my brain I *don't* have.
Which is why having a logical thinking coder close to hand saved me a couple of times!
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io