简体   繁体   中英

How do I know that an object is not colliding with another object without using OnCollisionExit in c# unity?

I tried using OnCollisionExit , but it doesn't detect the other object's box collider 2D being disabled (When I disable the other objects box collider 2D it doesn't detect it as stopping being collided). I need the other objects collider to be disabled because I'm using it as a range indicator for punching and I don't want it to interact with other objects(example: The player pushes away the enemy with his range indicator). Is there another method I could use?

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.tag == "PunchRange")
    {
        Player.GetComponent<Fight>().PunchInRange = true;
    }

    if (collision.collider.tag == "KickRange")
    {
        Player.GetComponent<Fight>().KickInRange = true;
    }
}
private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.collider.tag == "PunchRange")
    {
        Player.GetComponent<Fight>().PunchInRange = false;
    }
    if (collision.collider.tag == "KickRange")
    {
        Player.GetComponent<Fight>().KickInRange = false;
    }
}

I want it to detect whenever it is not colliding with an object even when the object's box collider 2D is disabled.

You have to restructure your introduction as it is so convoluted idk what you exactly want.

To detect collision or its lack you have to use colliders(maybe put additional collider but as a trigger). However what I understood is that you want to use them as triggers. On Collider2D component there is value isTrigger that you can set. Setting it makes the collider still work but not phisically (other colliders can pass thru it). Set that on the collider and use: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerExit2D.html

That should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM