Home > Net >  Is there a way to make a hitbox ignore another one?
Is there a way to make a hitbox ignore another one?

Time:02-28

I was creating a multiplayer first-person medieval fighting game based on Skyrim and For Honor combat, with 3 different stances. In it, you can attack from 3 stances, and if the opponent is not holding the same stance, he takes the attack, otherwise he parries and the attacker takes a quick stun. The problem starts when the Sword's Hitbox hits the player's own hitbox, causing unwanted results.

That's why I want to know if there is a way for making the player's sword ignore the collision with the player itself, so that he can't attack himself.

I also tried to put the method: Physics.IgnoreCollision(collider, selfCollider); Inside the OnTriggerEnter() function, Startup() and Update(), and no solution worked.

    private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Enemy" || other.gameObject.tag == "Player")
    {
        //instant enemy combat
        CombatManager enemy = other.gameObject.GetComponentInChildren<CombatManager>();
        Debug.Log(enemy.currentStance);
        //If enemy stance is not the same of the attacker
        if(enemy.currentStance != this.currentStance)
        {
            //enemy is damaged
            //Get enemy LifeManager and subtract its health
            enemy.GetComponentInParent<LifeManager>().currentHealth -= swordDamage;
        }
        else
        {
            anim.SetTrigger("Parried");
        }
    }

CodePudding user response:

Try:

if(other.transform.root != transform.root)
{
    // NOT self collision
}

This basically checks if both objects belong to the same hierarchy. In other words if they have the same top-most GameObject or root.

Obviously I made some presumptions as to how your players avatars are rigged. Perhaps you will need to tweak this solution to suit your hierarchy.

CodePudding user response:

You can decide which collision layers collide with others. Here is the link to the documentation.

  • Related