- Home /
When player enters triggerzone call function
Hi,
I seem to have a problem when entering a triggerzone. What i am trying to achieve is, whenever the player enters a certain zone (a well lit zone) What i tried to do is, put a sphere collider on the point light and attach this script:
var playerSanity : playerSanity;
function OnTriggerEnter(other : Collider) {
if(other.gameObject.CompareTag("Player"))
{
Debug.Log("TEST");
playerSanity.RegenerateFear();
}
Also use the collider as a trigger.
But this doesn't work. I also tried some tutorials that i could find on google. But they used the same method and didn't work either. Does anyone know a way how to achieve this?
Thanks,
Darryl
Is it possible that if i purchase Playmaker, that this would work? Cause i saw a tutorial which explains basically what i need. But if Playmaker uses the same method, there will probably be a high risk that it will not work. But if it uses another method, than it probably will. Because i still don't know what the problem is. I just cant figure it out.
Try changing it to
Debug.Log(other.gameObject);
Debug.Log(gameObject);
you must have something else in the scene tagged as player, or something else with this script attached, or $$anonymous$$AYBE your collider is scaled too big, try lowering the size of the collider component on the object
I don't know about Playmaker but this is a solvable issue, some detail is being overlooked is all
am I correct that the issue is still it activates on Start? this can ONLY mean the trigger is in contact with something tagged as Player, maybe the light, or one of its components
see what the above debug tells you
I will take a look maybe you are right, that there are multiple objects tagged as player. Or something else. I'll also check the Debug.Log, what is it tells me. And once i have that checked. I'll post results here.
Thank you!
no prob, hang in there!
by the way, something else I noticed since my prior advice..
setting the trigger to "is$$anonymous$$inematic" is a BAD idea, leave is$$anonymous$$inematic unchecked, and be sure to turn off gravity, otherwise I've found the trigger won't work unless it $$anonymous$$OVES (collision matrix doesn't mention that part)
but only an issue if the player does NOT have a rigidbody
otherwise, I find the collision matrix to be a very useful guide
Well i got it to work finally! I used the standard method. Same setup, different project. I think it had some problems with a script or an object in my old project. Cause right now it works like a charm. And i can finally wrap this feature up. Thanks for helping my out on this one Seth!
Answer by Seth-Bergman · Jul 25, 2012 at 07:06 PM
var playerSanity : playerSanity; //type and identifier same? If the data type is
//"playerSanity", the var name needs to be something different
function OnTriggerEnter(other : Collider) {
Debug.Log("entered trigger"); // see if the trigger was entered before checking tag
if(other.CompareTag("Player")) //don't need "gameObject"
{
Debug.Log("TEST");
playerSanity.RegenerateFear();
}
}
not sure what the problem is though. See if adding a Debug.Log as above gets called, to rule out issues with the tag..
EDIT : sorry, forgot to mention before
You also need to have a rigidbody on one of the objects..
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
the matrix at the bottom of this link will explain what types of objects can send trigger message
just add a rigidbody to the trigger object and set "isKinematic" true
EDIT: now I think DO NOT set isKinematic true, this may cause the trigger not to work (unless it moves) as described by my experience here
but that's only if the player does NOT have a rigidbody, I believe
I tried your script, but it didn't worked either. I can walk through the trigger collider, but the debug log before tag is checked isn't showing up. I thought something like this, was supposed to be simple.
Thanks anyway, i am going back to google. See if i can find any tutorials or reference scripts.
I just tested the ones on the reference site, they didn't worked either. I also tried to use OnControllerColliderHit on the character controller. But the same problem. I tested the reference ones on the Player itself and on other game objects. It shows no debug log or anything.
You also need to have a rigidbody on one of the objects..
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
the matrix at the bottom of this link will explain what types of objects can send trigger message
just add a rigidbody to the trigger object and set "is$$anonymous$$inematic" true
The only one that is sort of working is the kinematic one. I tried this a few hours ago, but it gave me a message even though i was not in the trigger zone. It also didn't respond when i entered.
sounds like the collider was just in the wrong position somehow. that's definitely the way to accomplish this
try again, and keep an eye on where the collider is, the only way you entered that function is by hitting a trigger
Your answer
Follow this Question
Related Questions
On Trigger enters being called a frame late 1 Answer
How do I prevent multiple triggers using OnTriggerEnter? 1 Answer
OnTriggerEnter Multiple Collisions Activating. I tried many variations, Please help (Java) 3 Answers
Can't call GetKey inside OnTriggerEnter? 2 Answers
Hello, How would one make a particlesystem activate triggers? (hopefully I worded that right) 0 Answers