- Home /
Unity getting an object to pass through another object but still trigger collision
basicaly i need an object to be able to pass through an object just like as if it was a trigger, but still detect it as a colision becouse of the way i made hitting enemies work (or a way to get physics2d.overlapboxall to detect triggers), here is the code for my weapon attack and the enemy i need to pass through the player but also do damage to the player. if (AttackDelay <= 0) { //attacking Collider2D[] EnemiesToDmg = Physics2D.OverlapBoxAll(AttackPos.position,new Vector2(AttackRangeX,AttackRangeY),0, WhatIsEnemy); for (int i = 0; i < EnemiesToDmg.Length; i++) {EnemiesToDmg[i].GetComponent<Health>().takeDamage(AttackDamage);}
and for the enemy
void FixedUpdate()
{
RotateAndMove(target2);
if (AttackRecharge<=AttackRechargeMax)
{
AttackRecharge += Time.deltaTime;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.layer==10)
{
player.gameObject.GetComponent<Health>().takeDamage(20);
}
if(collision.gameObject.transform==target2)
{
if (!IsVisible)
{
target2.position = player.position + (player.position - transform.position) / 5;
}
else
{
TeleportToCamera(target2);
}
}
}
Answer by GreatOscarF · Apr 17, 2020 at 10:13 PM
What's wrong with OnTriggerEnter2D, should work just fine...
the way i have it setup atm it ontriggerenter wouldnt work, and would require a bunch of remakeing of the way weapons work, and also in the future i plan to make aoe weapon attacks which for which the easiest way ik how to do it is the way its setup right now
Your answer
![](https://koobas.hobune.stream/wayback/20220612231727im_/https://answers.unity.com/themes/thub/images/avi.jpg)