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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

OnTriggerStay Ignoring Certain Colliders

Discussion in 'Physics' started by Supermoo48, Jan 3, 2018.

  1. Supermoo48

    Supermoo48

    Joined:
    Jan 3, 2018
    Posts:
    4
    I’m making a game where the player needs to get into thr car in a certain radius of it. I made the radius with a Trigger using OnTriggerStay. So when I’m in the trigger if I push E I get in the car. However if I push E when I’m not even close the the car it puts me in it, because the colliders in and around the car are activating it. So I’m trying for the OnTrigger Stay to only recognize the players colliders. Any ideas?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,732
    This really belongs in the scripting forum BUT you know how in your OnTriggerStay you have a Collider parameter? Well, try setting the player object with the collider on it to have a "Player" tag. Then you can just use the following code to detect said tag:

    Code (CSharp):
    1. if(collision.gameObject.CompareTag("Player"){
    2.  
    3. //Insert the code you use to get into the car here
    4.  
    5. }
     
    Last edited: Jan 4, 2018
    Ryiah likes this.
  3. Supermoo48

    Supermoo48

    Joined:
    Jan 3, 2018
    Posts:
    4
    Thanks. I’m new here and had no clue where to post this. Thank you!
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I dont think tags with "magic" strings should be used. First make sure to have a good collision matrix. Only relevant layers should collide. Then make sure to make a domain that is grouped in a good way so testing transforms/colliders is easy..

    Code (CSharp):
    1.         public void OnCollisionEnter(Collision collision)
    2.         {
    3.             var collidable = NVRInteractables.GetInteractable(collision.collider) as IPhysicalHandCollidable;
    4.  
    5.             if (collidable != null)
    6.             {
    7.                 collidable.OnCollide(hand, collision);
    8.             }
    9.         }
    This is a O(1) operation since GetInteractable uses a dictionary. Then any implementation that implements NVRInteractible can implement IPhysicalHandCollidable to subscribe to collsion to the players physical hand (This is a VR game). Here an example were the firearm slide is returned if its hit hard enough and in the right direction

    Code (CSharp):
    1.         public void OnCollide(NVRHand hand, Collision col)
    2.         {
    3.             if(IsLockedBack)
    4.             {
    5.                 var force = Vector3.Project(col.relativeVelocity, SlamDirection.up);
    6.  
    7.                 var angle = Vector3.Angle(SlamDirection.up, col.relativeVelocity);
    8.  
    9.                 if (angle < 60 && force.magnitude > 0.25f)
    10.                 {
    11.                     ReturnSlide();
    12.                     hand.ForceGhost();
    13.                 }            
    14.             }
    15.         }
    We use this all the time for different things in our game. Works very well, plus its stronly typed
     
    Supermoo48 likes this.