When an enemy fires a projectile and it hits another enemy I would like the projectile to impact the enemy, create an impact particle, and be destroyed. I have created a script which successfully does this in testing, but when an enemy actually fires the projectile it is destroyed right away. This is what I have:
{
public GameObject impactParticle;
public Vector3 impactNormal;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
impactParticle = Instantiate(impactParticle, transform.position,
Quaternion.FromToRotation(Vector3.up, impactNormal)) as GameObject;
Destroy(gameObject);
}
}
}
I thought the use of ‘other’ would mean the enemy firing the projectile would be ignored, but this apparently isn’t the case?
Alternatively, I would not use the Destroy method. I would create the prefab to destroy its self on completion. I think that stuff is still in the particle system.
I guess the projectile is colliding with the enemy who is firing it which is why it triggers instantly. I have found a quick workaround, if I disable the sphere collider of the projectile and enable it via script after 0.18 seconds the projectile explode trigger isn’t set off prematurely. I will have to playtest and see if this is going to be a good solution in the long run.