- Home /
Object hit with multiple shotgun bullets only taking damage from one bullet?
private void OnCollisionStay(Collision collision)
{
if (collision.transform.tag == "Bullettag")
{
if (collision.gameObject.GetComponent<BulletScript>().playerbullet == true)
{
attackedByPlayer = true;
player = collision.gameObject.GetComponent<BulletScript>().player;
}
HP -= ((collision.gameObject.GetComponent<BulletScript>().firedamage) * (1-(fireresist/100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().icedamage) * (1 - (iceresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().ballisticdamage) * (1 - (ballisticresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().bluntdamage) * (1 - (bluntresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().chaosdamage) * (1 - (chaosresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().electricdamage) * (1 - (electricresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().normaldamage) * (1 - (normalresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().poisondamage) * (1 - (poisonresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().psychedamage) * (1 - (psycheresist / 100)));
HP -= ((collision.gameObject.GetComponent<BulletScript>().aciddamage) * (1 - (acidresist / 100)));
}
}
I have a script that's meant to have an enemy take damage from a bullet when they're hit by the bullet. This code works perfectly fine for individual bullets, but when I try to make a shotgun (with about 6 projectiles) the enemy is only ever hit by one bullet at a time, since its health only decrements by the damage of one bullet. How would I get around this?
Answer by cgarossi · Sep 01, 2017 at 08:46 AM
A way around this is to move your health decrement logic into the bullets it self. Instead of your target dealing with what hit it, move the logic into the bullet, which applies a modifier to the hit object.
This way, each bullet is responsible for dealing damage to the hit object, not the hit object working out who hit it and what damage was caused.
That way you could create a shotgun with 30 bullets firing at the same time, each individual bullet would then apply damage.
my bullets work opposite - i have one bullet but it applies multiple damage. i have not only tried to destroy the bullet on collision but also create a toggle so that the bullet can only run the oncollision() once. Here is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class Bullet$$anonymous$$ove : NetworkBehaviour {
private Rigidbody rb;
private bool hit = false;
[SerializeField] private float setTimeTillDestroy = 5f;
void Start() {
rb = gameObject.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 500, Force$$anonymous$$ode.Impulse);
Destroy(gameObject, setTimeTillDestroy);
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
if(hit == false)
{
col.gameObject.GetComponent<Health>().CmdTakeDamage(5);
NetworkServer.Destroy(gameObject);
hit = true;
}
}
}
}
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D and OnCollisionExit2D executed at the same time 0 Answers
Colliding with multiple objects 1 Answer
how to make an Explosive rigidbody trigger by another collision box 0 Answers
Third Person Camera Can See Through Walls! 1 Answer
OnCollisionEnter2D(coll: Collision2D) function not being triggered 0 Answers