Arrows not stopping immediately when target hit?

Hello!

I am making a small FPS where you shoot arrows at enemies. I want to make it so that arrows stick whenever they hit any object and, if they hit an enemy, they should stick to the enemy.

However, after making several different attempts at making this happen using different object-stopping code, the arrows seem to keep stopping AFTER they collide with the enemy. One would assume that the arrow-stopping code is kicking in too late, but here is what is strange: this bug happens only occurs sometimes whereas in other cases the arrows stick to the enemy correctly. The distance some of the arrows travel is also sometimes way too long.

Here is a picture of the bug in Scene view and the code I use to make the arrow stop.

Thank you very much!

private void OnCollisionEnter(Collision collision)
{
flying = false;

    rb.isKinematic = true;
    rb.velocity = Vector3.zero;
    rb.angularVelocity = Vector3.zero;

    //rb.constraints = RigidbodyConstraints.FreezeAll;
    transform.rotation = rotationWhenEnemyHit;
    rb.useGravity = false;

    if (collision.gameObject.tag == enemyTag)
    {

        transform.SetParent(collision.gameObject.transform);
        enemyHitStats = collision.gameObject.GetComponent<EnemyCombatStats>();
        enemyHitStats.TakeDamage(arrowDamage);
        enemyHit = true;

    }
    else
        hitButNotEnemy = true;

    collider.enabled = false;
         

}

It’s possible the arrow is traveling that much further through the enemy in one frame, though I thought discrete continuous would help that…

Try setting the arrow position to the point of collision. My guess is you’d set .position (not .localPosition) to this value after reparenting to the hit collider. You may want to iterate through all the contact points to make sure you get the one with the right enemy tag on it.

I also think you only need to set isKinematic to stop most of the other rb properties you disable, once you prove it working.

Also I’m sure you know, the character doesn’t have a mesh collider? It looks like you’re using a capsule collider that’s not exactly aligned with your player. I’m assuming that’s temporary :slight_smile:

Try changing the Rigidbody’s detection parameter in the Inspector to be Continuous Dynamic instead of default Discrete.

@BradyIrv I’ve made sure of that both in the prefab and in code when spawning the arrow. You can see the mode is set to Continuous Dynamic in the bottom right of the screen below (I shot the arrow and paused the game before it hit any colliders)