- Home /
Triggers are being activated by playerController when the Controller is already inside the trigger.
Hi all-- This is a bit of an odd one. I've spent a few days trying to figure it out. The game is 2D but it's still using the 3d physics engine. My character can shoot a gun. So let's take a sliding door as an example of this trigger problem--it affects all triggers. When the player enters the trigger--the door opens. When the player leaves the trigger, the door closes. Simple and to the point. The issue--whenever the player fires the gun--the onTriggerEnter, and onTriggerExit IMMEDIATELY activate back to back and the door will open and close while the player is still within the trigger space--never having left it.
The only thing I can decipher--is Instantiating something (a bullet) from the player controls script causes the controller's collider to reset? The triggers are activated by a SendMessage(ActivateTrigger) --ActivateTrigger being my custom script the trigger objects--like doors--share. Only the player script has functions that SendMessage with Activate Trigger -- so the problem has to be with the player.
Things I know: The same effect occurs when I resize the characterController's capsule collider in the editor window -- shooting a bullet doesn't resize the characterController.
After another test--I'm leaning towards thinking it has something to do with instantiating a new object from the player script--- I just don't know why. Is that typical behavior? The objects I'm instantiating do not have scripts that can activate the triggers--and furthermore--even if they shared the same layer or tag as the parent player script that made them--it still wouldn't activate the triggers.
Check to see the collider has a tag of Player. For this you'll have to tag your controller as a "Player". void OnTriggerEnter(Collider other) { if(other.tag == "Player") { //Do your door opening code here } }
Hope this helps something :D
door collider has tag "door" player's instantiated bullet has tag "playerBullet" and the player collider as the tag "Player"
Right now my OnTriggerEnter and OnTriggerExit functions are in the player's script.
OnTriggerEnter(collider other){
if(other.tag == "door"){
other.Send$$anonymous$$essage("ActivateTrigger", Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
the instantiated bullet script doesn't work off triggers--they shoot raycasts and have no OnTriggerEnter / Exit code--and any messages sent to colliders are sending a completely different Send$$anonymous$$essage() that has zero effect on the door trigger.
I'm about to try another test though.
EDIT: The OnTriggerExit() call is exactly the same as the OnTriggerEnter() here. The door script checks what state it's in open/close and simply reverses it. $$anonymous$$y point is that both functions are identical in the player script.
So I tested a new method:
I created a function attached to an arbitrary keyboard key and when pressed, it instantiates a static collider object--it's just a quad with a texture, and a box collider. Whenever it's instantiated from the player script--it re-triggers the doors. It has no script attached, and it's tag is "tile".
The only thing I can reason is that somewhere in the instantiation process, the object being created is temporarily treated as an extension of the player's collider before treating it as a separate object. This seems like a bug to me. I'm going to run another test in a $$anonymous$$ute where I instantiate the object outside the player's script e.g. Send$$anonymous$$esssage to a different object with a different script to instantiate the object ins$$anonymous$$d of the player. This is killing me right now.
Axe that last comment----the objects were instantiating on the player, so they were just forcing the player to move--when I change it to instantiate outside the players collider, the test objects don't trigger the door's trigger.
So instantiated objects aren't inheriting the parent creators properties....the bullets aren't instantiating in the player's collider either--so it must be something in the bullet script. I'll keep looking and get back.
Answer by robcbryant · Oct 26, 2015 at 09:59 PM
Okay--I figured it out. It's a little weird--and potentially a bug? Can someone please tell me whether or not I should report it?
Physics.IgnoreCollision(GetComponent<Collider>(), playerObject.GetComponent<Collider>());
Here's the culprit. When the bullet is instantiated, it makes a physics call to ignore the player's collider in Awake(). When that is commented out--problem solved. What I'm guessing is happening is that the Physics.IgnoreCollision() function temporarily disables the colliders given as parameters, runs some physx code on them, and then re-enables them in the background of Unity--the effect is that if a player controller's collider is within a Trigger box --e.g. after OnTriggerEnter is called but before OnTriggerExit is called -- the physics function disables the player collider which triggers an OnTriggerExit, and then renables the collider which triggers an OnTriggerEnter.
That seems like a bug to me--but maybe that's just how it's supposed to act?
hahaha. I'm such a dope. here we are:
"Note that IgnoreCollision will reset the trigger state of affected colliders, so you might receive OnTriggerExit and OnTriggerEnter messages in response to calling this."
http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
Your answer
Follow this Question
Related Questions
How to pick up a game object using E 4 Answers
Kill Particles Entering 2D Trigger 1 Answer
How to keep track of multiple gameobjects inside collider 0 Answers
player on trigger enter dont work? 1 Answer