Find both colliders involved in a collision on GameObject with Multiple colliders

前端 未结 2 1882
花落未央
花落未央 2020-12-12 06:01

To sum up the following: I want to find both colliders involved in a collision, from an OnTriggerEnter2D event. How can I do this?

I have two gameob

相关标签:
2条回答
  • 2020-12-12 06:31

    One way to achive this is to create a child gameobject to handle one of the colliders.

    So for example you have a parent object that has a non-trigger collider, and a child with a trigger collider.

    This way its easy to figure out wich colliders are involved in the collision.

    0 讨论(0)
  • 2020-12-12 06:38

    To sum it up, I want to find both colliders involved in a collision

    There is a gameObject variable declared when your script inherits from MonoBehaviour. This variable refers to the GameObject this script is attached to. You can get one GameObject with that gameObject variable and the other one from the Collider2D argument in the OnTriggerEnter function.

    void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject obj1 = this.gameObject;
        GameObject obj2 = collision.gameObject;
    
        Debug.Log("Triggered Obj1: :" + obj1.name);
        Debug.Log("Triggered obj2: :" + obj2.name);
    }
    

    EDIT:

    The objects are useless to me. I need the colliders. And no, I can't use 'getcomponent' because they have more than one collider, and I need only the ones in the collision

    The colliders should be made child of the GameObject and the script must be attached to each child collider then what's in this answer should work.


    If some reason you must do this without making the colliders child of that GameObject then use a boolean variable to detect the collision once only.

    This is a modification of an answer from this post.

    Have a local Collider2D variable named theOtherCollider to store the collision data first reported when OnTriggerEnter2D is called then have a another boolean variable named detectedBefore to determine if the OnTriggerEnter2D has been called before.

    When OnTriggerEnter2D is called, check if the local/this version of that boolean variable is false. If it's not true then this is the first time OnTriggerEnter2D has been called. Use GetComponent to get the other script then set the boolean variable of the other script to true. At the-same time, also initialize the theOtherCollider variable on the other script with the Collider2D value from the OnTriggerEnter2D function.

    Now, if OnTriggerEnter2D is called and the local/this version of that boolean variable is true, set it to false to reset it then obtain both colliders with theOtherCollider variable and the Collider2D variable from the OnTriggerEnter2D function.

    This may be confusing but by looking the code, it is easier to understand.

    Note:

    YOURSCRIPT is the name of the script the OnTriggerEnter2D function is inside and that is attached to the Colliders. You must change it to whatever that script is named.

    public bool detectedBefore = false;
    public Collider2D theOtherCollider;
    
    void OnTriggerEnter2D(Collider2D collision)
    {
        //Get both colliders then exit if we have already ran this code below
        if (detectedBefore)
        {
            //Reset
            detectedBefore = false;
    
            //Get both Colliders once below
            Collider2D col1 = theOtherCollider;
            Collider2D col2 = collision;
    
            Debug.Log("Triggered Obj1: " + col1.name);
            Debug.Log("Triggered obj2: " + col2.name);
    
            return; //EXIT the function
        }
    
        //Set the other detectedBefore variable to true then set get the first Collider
        YOURSCRIPT myScript = collision.gameObject.GetComponent<YOURSCRIPT>();
        if (myScript)
        {
            myScript.detectedBefore = true;
            myScript.theOtherCollider = collision;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题