- Home /
[SOLVED]Different levels of damage per particle collision
I have a mech unit that shoots out 4 different particles that are set with collisions in order to damage an enemy. How do I code in 4 different levels of damage, one for each particle effect. I know how to make all particles do only one level of damage.
My code that you see here does not work. If I shoot the enemy with either 4 of the weapons the robot catches fire which is not intended. It should only catch fire when being hit by flameHitL or flameHitR.
My script does NOT work to change damages. It seems to add on the damages such in a way that the first if statement (shooting the enemy with the flameHitLR does its damage PLUS the damage of the rest of the if statements. (so 1 + 20 + 5) and kills the enemy instantly. The rest of the weapons don't seem to do any damage and they still set the enemy on fire. This script is on the enemy. Please take a look and see what I am doing wrong. I probably don't understand how the if statement work with collisions in this way.
[SerializeField] int hitPoints = 100; //how many hitpoints of life the enemy has
[SerializeField] Collider collisionMesh; //enemy's collider
[SerializeField] ParticleSystem robotFlames; //particles that play when enemy is hit with flamesHitL or flamesHitR ONLY
[SerializeField] ParticleSystem deathExplode;//particle system that plays when the robot runs out of hitpoints (death explosion)
[SerializeField] ParticleSystem flameHitL; // |
[SerializeField] ParticleSystem flameHitR; // |
[SerializeField] ParticleSystem bigGunHitL; // Different types of particles each having a left and right
[SerializeField] ParticleSystem bigGunHitR;// |
[SerializeField] ParticleSystem shotGunHitL;// |
[SerializeField] ParticleSystem shotGunHitR;// |
[SerializeField] ParticleSystem grenadeExplosion;// grenade explosion particles
void OnParticleCollision(GameObject other)
{
// asking if the enemy is being hit by either flamehitL or flamehitR the hit damage should be 1 per particle collsion
if (flameHitR || flameHitL)
{
hitPoints = hitPoints - 1;
robotFlames.Play(); // flamethrower hits enemy... enemy should catch on fire.
}
// asking if the enemy is being hit by either bigGunHitL or bigGunHitR the hit damage should be 20 per particle collsion
if (bigGunHitL || bigGunHitR)
{
hitPoints = hitPoints - 20; // enemies should NOT catch fire when being hit by normal bullets.
}
// asking if the enemy is being hit by either shotGunHitL or shotGunHitR the hit damage should be 5 per particle collsion
if (shotGunHitL || shotGunHitR)
{
hitPoints = hitPoints - 5;
}
}
Answer by jfn73150 · Aug 22, 2018 at 04:44 PM
ANSWER
If anyone is having the same experience there is a much simpler solution.
Make a script for Particle Damage. Set that script on each actual particle system on your character.
In that script simply add -
[SerializeField] int particleDamage = 1; //MUST BE SERIALIZEDFIELD
public int GetDamage()
{
return particleDamage;
}
Then in your enemy damage script write -
void OnParticleCollision(GameObject other)
{
int damage = other.GetComponent<ParticleSystemDamage>().GetDamage(); //GetDamage()grabs damage level from each separate particle system component with the particle system damage script added to it.
ProcessHit(damage);
if(hitPoints <= 1)
{
KillEnemey();
}
}
void ProcessHit(int damage)
{
hitPoints = hitPoints - damage; //processes damage from GetDamage() above
print("current hitpoints are " + hitPoints);
}