Search code examples
c#unity-game-enginecollisionlayerprojectile

Walking on projectiles bullets, not colliding but player jumps on them due to step offset


Title says it all, the projectiles only hit the player and disappear if the player is standing still, if he is moving, the player will ''jump'' on top of the bullets and walk over them, not triggering a collision and not doing anything really.

What Im trying to achieve is that the bullets will passthrough the player, but still detect collision if they do collide with the player, so that the character controller can never walk on them, or they should just collide with the player before the player gets the chance to walk over them. This problem has to do with the "Step Offset" function in the character controller component, because when I set it to 0.05 this issue does not happen, but I cant do that because my character needs to be able to walk up stairs or over small ledges.

Thanks a lot!

void OnCollisionEnter(Collision other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        scrCh = other.gameObject.GetComponent<scr_CharacterController>();
        calculatedDamage = Random.Range(minDamage, maxDamage);
        if (scrCh != null && scrCh.currentHealth > 0)
        {
            CancelInvoke("Remove");
            gameObject.SetActive(false);
            scrCh.TakeDamage(calculatedDamage);
            


            if (gameObject.CompareTag("Special Bullet"))
            {
                scrCh.TakeDamage(calculatedDamage * 3);
                gameObject.SetActive(false);
            }
        }
    }

    if (other.gameObject.CompareTag("Unbreakable"))
    {

        gameObject.SetActive(false);


    }
}

Solution

  • There are 2 ways to fix this issue:

    1. Either change the size of the Character Controller Collision Box(Its a Cylinder), so that its size is smaller than your Character Mesh Collider, this way the bullets will collide with the Mesh instead of with the Character Controller.

    2. Or you can set the Character Controller on a different Layer, one that does not collide with the bullets, and the result is the same the bullets will now only collide with the Character Mesh Collider.