OnTriggerStay Ignoring Certain Colliders

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?

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:

if(collision.gameObject.CompareTag("Player"){

//Insert the code you use to get into the car here

}
1 Like

Thanks. I’m new here and had no clue where to post this. Thank you!

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…

        public void OnCollisionEnter(Collision collision)
        {
            var collidable = NVRInteractables.GetInteractable(collision.collider) as IPhysicalHandCollidable;

            if (collidable != null)
            {
                collidable.OnCollide(hand, collision);
            }
        }

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

        public void OnCollide(NVRHand hand, Collision col)
        {
            if(IsLockedBack)
            {
                var force = Vector3.Project(col.relativeVelocity, SlamDirection.up);

                var angle = Vector3.Angle(SlamDirection.up, col.relativeVelocity);

                if (angle < 60 && force.magnitude > 0.25f)
                {
                    ReturnSlide();
                    hand.ForceGhost();
                }            
            }
        }

We use this all the time for different things in our game. Works very well, plus its stronly typed

1 Like