Other Object's Collider Giving me Trouble (Solved)

So I’m making a simple hack and slash game in C#. I have two colliders on each enemy their physical collider so the player can’t run through them and an “aggro” collider that makes the enemy interact with the player when he enters. The player also has two colliders their physical collider and their targeting collider. For targeting the enemy I have this code

void OnTriggerEnter(Collider other)
{

if (other.gameObject.CompareTag (“Skeleton”))
{

{
enemyinrange = true ;
target = other.gameObject ;
skelehp = target.gameObject.GetComponent ();

}

}
}

void OnTriggerExit(Collider other) {

if (other.gameObject.CompareTag (“Skeleton”))
{
enemyinrange = false ;

}
}

void OnTriggerStay(Collider other)
{

if (other.CompareTag (“Skeleton”)) {
enemyinrange = true;

}
}

void FixedUpdate ()
{
if (Input.GetButtonDown (“Fire1”) && enemyinrange)
{
skelehp.skeletonhp = skelehp.skeletonhp - 5;

}
if (skelehp.skeletonhp == 0)
{
Destroy (target);
enemyinrange = false;
}
}

So my problem is whenever I enter the enemies “aggro” collider which is a lot bigger than their physical collider, I can do damage to them. Is there a way I can get it so when my targeting colldier enters the enemies physical colldier I can attack then? This is my first bigger project so I’m still unsure of some code or techniques through unity, hence why I’m asking for help.

Are both your Colliders on the same GameObject? If so, try using a separate GameObject for each Collider. You can then put those different GameObjects into separate layers and filter the Colliders using Edit → Settings → Physics so that only the colliders you want will interact with each other. Or you can use object tags and give them different tags as you seem to be doing already… but I think layers is the better way to go.

Okay that makes sense, I’m going to give it a whirl. I’m glad it wasn’t a scripting problem.

Yep, that fixed my problem thanks for the help

1 Like