- Home /
Applying damage with Flamethrower-like weapon
The weapon is actually a hand which shoots fire (much like in Skyrim). My first attempt at applying damage was through the Particle Collision, with the Send Collision Messages. This looked pretty much what i was looking for, but more testing surfaced a problem: The damage caused by the particles varied a lot and was influenced by fps, which was sub-optimal. My next attempt was with a box in the hand extending to the range limit (no mesh renderer) with a box collider. It applied damage to the enemies just like I wanted it to and was stable, but the problem was that if two(or more) enemies were in a straight line, it would damage both, which makes no sense as the flame stops at the first one. The third attempt was with raycast. This was perfect, except for the fact that it casted only one ray, in a straight line like a gun. The flame is fairly wide. This method made shooting flames a matter of aiming down. Also I failed in combining the 2nd and 3rd option. I then returned to the first option and the same problems plague me.
function throwFlame(rast : boolean)
{
if(PlayerStats.GetWill() > 0)
{
damageCollider.SetActive(rast);
// var flameHit : RaycastHit;
// var fwd = flame.transform.TransformDirection (Vector3.forward);
PlayerStats.AddWill(-damageFireCost / 10);
// if(Physics.Raycast(flame.transform.position, fwd, flameHit, 5))
// {
if(enemyCol != null && enemyCol.transform.tag == "Enemy")
{
if(enemyCol.GetComponent.<ParticleSystem>() != null)
{
enemyCol.GetComponent.<ParticleSystem>().enableEmission = true;
}
// var AIEnemy : Component;
// AIEnemy = enemyCol.GetComponent("AIEnemy");
// if(AIEnemy != null)
// AIEnemy.AddHealth(-20 * Time.deltaTime);
}
flame.GetComponent.<ParticleSystem>().enableEmission = rast;
}
else
{
flame.GetComponent.<ParticleSystem>().enableEmission = false;
flame.GetComponent.<ParticleSystem>().Clear();
}
}
Above is the script on my player. The commented parts are leftovers from my previous attempts.
Below is the script on the enemy that registers the damage:
void OnParticleCollision (GameObject other)
{
if (other.transform.tag == "ParticleFireShoot") {
health -= 2;
}
}
Thank you, any input is appreciated.
Answer by contact_unity364 · Jan 26, 2021 at 11:53 PM
The approach which I will use, is to get a list of enemies who are in front of the player, and within range of the flames. then work out the angle difference between your fire start point / forward vector and the enemy colliders. Then you have a distance and angle so you could work out some proportional damage based on those. I.e. directly infront and close would get hit by a lot of fire, where as further away and off to the side would get less. And cut off at ~15%.
If it was an issue, you could help out performance by maintaining a list of enemies who are roughly in proximity and only update that list every 1/3 of a second, and then only check the angle and distance every 1/6 second or something like that.
Your answer
Follow this Question
Related Questions
How to use a Raycast to see the distance traveled within a collider. 2 Answers
Move player to specific location and avoid obstacle. 2 Answers
Im trying to create a word game with a twist 0 Answers
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers
ball sometimes goes through the bat when hit... collider problem... How to do a Raycast? 0 Answers