I was working on a small game in which you shoot projectiles towards various targets, now I decide to use OnTriggerEnter instead of OnCollisionEnter because OnCollisionEnter adds a slight force to the target during collision and I don't want that(btw, if anyone knows how to fix that, it'd be great), so I had to use OnTriggerEnter instead.
So the problem is that my code works just fine when the target is still or static. But when the target is in motion, sometimes the projectile works just fine as wanted(damages the target and disables itself afterwards) and other times, the projectile literally just moves through the target without causing any damage.
I say "sometimes" because this happens completely randomly.
Here's is my code:
public int m_DamageAmount;
public int speed;
private Rigidbody m_Rigidbody;
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
m_Rigidbody.AddForce(transform.forward * speed, ForceMode.Impulse);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
ObjectHolders._ImpactFX.Play();
ObjectHolders._ImpactFX.transform.position = other.transform.position;
EnemyBrain enemy = other.GetComponent<EnemyBrain>();
if (enemy != null)
enemy.Damage(m_DamageAmount);
gameObject.SetActive(false);
}
}
Here are other things I've tried:
1) I have changed both the target and the projectile's movements from transform to rigidbody.
2) I have tried changing my game time's Fixed Timestep value from 0.02 to 0.1.
These things did not have any visual effects on it.