Question by
unity_772A4BD2401AFB601928 · May 22 at 09:01 PM ·
collisionprefabontriggerenterbulletbullet-prefab
How to make a prefab bullet to knock back and penetrate an enemy at the same time?
Question: I am trying to make a bullet that will penetrate and knock back the enemy at the same time, like this: https://gyazo.com/2cac12425c4c684e828ff0b0389d97db
What I did here is instead of using the OnCollisionEnter function, I use the OnTriggerEnter function and set the collider to a trigger. However, this doesn't work really well, the OnTriggerEnter is a lot slower than the OnCollisionEnter function and sometimes the bullet just goes through the well and the enemy... What can I do to fix it?
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Wall")
{
gameObject.GetComponent<Rigidbody>().velocity= new Vector3 (0,0,0);
GameObject effect = Instantiate(muzzleFlash, transform.position, Quaternion.identity);
Destroy(effect, 5);
Destroy(gameObject,0.5f);
}else if(other.gameObject.tag == "Enemy")
{
Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
dir.y = 0;
rb.AddForce(dir.normalized * KnockbackStrength, ForceMode.Impulse);
}
else
{
Destroy(gameObject, 2f);
}
}
Comment