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;
}