Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Logical Colliders

Discussion in 'Physics' started by eshalev, Aug 18, 2016.

  1. eshalev

    eshalev

    Joined:
    Dec 16, 2015
    Posts:
    4
    Hi,
    I want a car which can change driving lanes. To do this, I want the car to check if the lanes next to it are free of other cars. I want to have car which can check if the traffic lanes next to it are free of other cars. I would like to do this by adding a "logical" colliders next to the car to check if other cars overlap with them.

    How can make it that the colliders recieve onEnter events, but do not effect the other car's physics? I know that I can set them to be triggers, however then only the other cars will recieve the "onTriggerEnter" event (instead of the car which owns those triggers). While I can easily overcome this by exchanging messages between the cars, it would be much nicer if the car that "owns" the colliders was the one receiving the messages in the first place. As a novice user, I am a bit confused about the abundance of behaviors I can attach to my colliders (Trigger, kinematic, rigidbody, etc...).

    Is there some combination which would achieve the behavior I want?
    lanes.png
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    eshalev likes this.
  3. eshalev

    eshalev

    Joined:
    Dec 16, 2015
    Posts:
    4
    I never thought to make them a child of the car's rigid body, as technically they are not a part of the car's physics. I only made them children of the parent car object. Does that matter?
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    Yes, that matters. Triggers need to belong to a rigidbody in order to receive the OnTrigger events. They are part of the car physics as they're used for detecting other physical elements in the vicinity of the car.
     
  5. eshalev

    eshalev

    Joined:
    Dec 16, 2015
    Posts:
    4
    It does not work for me.
    My object heirearchy is as thus.

    - Car (an empty object with a-lot of behavioral scripts attached to it)
    -- CarBody ( [child of car] - contains a mesh and a rigidbody )

    It is important to say that at this point all physics (without sensors) works fine. Cars move, collide and render as I would expect them to.

    Then I created a "FrontSensor" object, which is basically a box with a collider set with isTrigger=True.
    I linked a simple c# script to the FrontSensor object:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ColliderSensor : MonoBehaviour
    6. {
    7.  
    8.    ...
    9.  
    10.     public void OnCollisionStay(Collision collision)
    11.     {
    12.         print("Colliding with" + collision.gameObject.name);
    13.     }
    14. }
    15.  
    Since I could find several ways to interpret your comment of "triggers rigged so they are children of the vehicle's rigidbody"
    1. First attempt was with the Frontsensor object as a child of Carbody. (Carbody is the object with a rigid body)
    2. Second attempt, I made "FrontSensor" object a direct child of "Car", and I gave it it's own RigidBody (in addition to the one in CarBody).

    In both cases no collision events were fired (I also tried scaling the FrontSensor so that it is ridciolously large to increase the number of collision opportunities [There are many other rigidbodies with colliders in the scene] )

    Just in case, this what the FrontSensor's Inspector Looks like on the second attempt. You can see that it is a trigger, and that the script is attached, and also that I have made the rigidbody kinematic (because I don't want it to move by itself)

    upload_2016-9-15_16-19-58.png
     
    Last edited: Sep 15, 2016
  6. Kamalen

    Kamalen

    Joined:
    Dec 27, 2012
    Posts:
    29
    Just a detail, but with trigger colliders, the method is named OnTriggerStay
     
    eshalev likes this.
  7. eshalev

    eshalev

    Joined:
    Dec 16, 2015
    Posts:
    4
    Thanks Tidius!
    That was not just a detail, it was what caused it to work.

    I assumed that since the script was attached to a trigger, and I was trying to detect "real" colliders, I thought it would be onCollision* Assumption turned out to be wrong....
     
  8. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    Be aware that large dynamic trigger colliders can have a performance impact. It might be better to use raycasts or boxcasts every Nth frame.