0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This sounds like "tunneling" - the collider is on one side of the object in one step, and the other side on the next step, and no collision happens in between.

Some solutions you can try:

  • Extend the collider for the bullet backward along the bullet's path, so even if it skips past an object in a single physics step, its "tail" catches it. This one's dirt cheap, but just needs a little attention to make sure you don't shoot yourself or something behind you.

  • Use a Shape Cast or Continuous Collision Detection to check a swept volume of space, rather than discrete snapshots. This is more expensive, but much more robust.

  • Reduce the speed of your projectiles (or, if they're meant to be super fast, consider using a hitscan raycast/shapecast for the real hit detection, and the projectile travel can be purely visual flourish)

\$\endgroup\$
1
  • \$\begingroup\$ This is really good, although, I tried the speed thing before, it wasn't really as visual realistic as I thought. So now I extended the height, length and width of the colliders on the projectiles and used code to check the target that it is allowed to damage, so far, it is working perfectly! Awesome! \$\endgroup\$ May 12, 2020 at 6:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .