Search Unity

Manage multiple trigger colliders from one script

Discussion in 'Physics' started by Deleted User, May 5, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    I'm coding my own car physics with custom wheel coding. The car is controlled by a single script and each tire has a mesh trigger collider.

    My question is, how can I pick which collider I control and get information from ? So far I've only written scripts that handle a single trigger. Can you please help me ?
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    If you have multiple Collider components on a single GameObject, they are a compound collider and act as one collider for event purposes. To check them separately, you would need to get the Collision from the other object, or use the contact point to check which Collider was hit. It will be difficult to respond to different components on the same object, though, since nothing distinguishes them reliably except for the instance IDs.

    If you have child objects with Collider's, it depends on whether the children also have Rigidbody's. When only the parent has a Rigidbody, collisions/triggers caused by interactions with the children will throw events in the parent. In this case, you can make use of tags or object names to act selectively in response to certain colliders. If any child has a Rigidbody, collision/trigger events will be on their own object rather than the parent. You won't be able to manage them all with one script, but you can write short scripts to call a function in the parent script from the children in response to a collision/trigger, so that you can at least keep all the logic in one place if you prefer things that way.
     
    Last edited: May 6, 2019
  3. Deleted User

    Deleted User

    Guest

    All the colliders are on separate gameobject. The wheels have kinematic rigidbodies.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I believe the suggestion I posted in this thread might apply here as well.
     
  5. Deleted User

    Deleted User

    Guest

    Write this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public interface ICollisionReceiver
    6. {
    7.     void OnTriggerEnterNotif(Collider other);
    8.     void OnTriggerStayNotif(Collider other);
    9.     void OnTriggerExitNotif(Collider other);
    10. }
    11.  
    Put this on your object that has the colliders and must send the message
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollisionBroadcaster : MonoBehaviour
    6. {
    7.     [SerializeField] public ICollisionReceiver triggerReceiver;
    8.  
    9.     private void OnTriggerEnter(Collider other)
    10.     {
    11.         triggerReceiver.OnTriggerEnterNotif(other);
    12.     }
    13.  
    14.     private void OnTriggerStay(Collider other)
    15.     {
    16.         triggerReceiver.OnTriggerStayNotif(other);
    17.     }
    18.  
    19.     private void OnTriggerExit(Collider other)
    20.     {
    21.         triggerReceiver.OnTriggerExitNotif(other);
    22.     }
    23. }
    24.  
    Then put something like this on the object that needs to do logic based on when collisions happen
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollisionReactor: MonoBehaviour, ICollisionReceiver
    6. {
    7.     public void OnTriggerEnterNotif(Collider other)
    8.     {
    9.     }
    10.  
    11.     public void OnTriggerExitNotif(Collider other)
    12.     {
    13.     }
    14.  
    15.     public void OnTriggerStayNotif(Collider other)
    16.     {
    17.     }
    18. }
    Then drag the reactor object into your broadcaster object's serialized field within the unity inspector window
     
    Last edited by a moderator: Nov 11, 2020
    edcxvhi0ol likes this.
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Interfaces cannot be serialized in the inspector, unfortunately.