- Home /
OnTriggerEnter (JS)
I have a script for an object. If the object hits my player it should load an other screen. The problem is, it only works when the player is moving. If the player doesn't move the application doesn't load.
Here is what I have:
function OnTriggerEnter(otherObj: Collider){
if (otherObj.tag == "Player"){
Application.LoadLevel("deathscreen");
}
}
Hope you know what the problem is.
Use OnTriggerStay
ins$$anonymous$$d. - OnTriggerEnter
fires only once, only when you first enter the trigger - i.e. it doesn't fire while you're standing inside the trigger.
@vexe - I don't think the problem is related to enter/stay. Please note, that before OnTriggerStay OnTriggerEnter should be called, so level should be loaded.
I don't know what happens if one collider is spawned inside other - maybe in this case OnTriggerEnter is omitted?
@GEWLAR - do you have any special code executed when your player starts/stops moving?
Yes, you are correct. I read the question in a hurry. I thought about my comment after I wrote it, OnTriggerEnter
should do the trick. @GEWLAR try OnTriggerStay
, if it works, it means that OnTriggerEnter
is getting ignored, in a way. And don't hesitate to show more relevant code.
I tryed OnTriggerStay
it doesn't work. I think the problem is that the function won't get called if the collider hits the player. It only gets called when the player hits the collider. But how to fix that ?
From what I know, it shouldn't matter who hits whom ^_^ - As long as there's a collision detection script/handler attached to either of the colliders.
Answer by clunk47 · Oct 31, 2013 at 05:06 PM
You're probably using a CharacterController.
If you want to use regular collision detection or trigger on a character it needs another collider component in addition to the charactercontroller, charactercontroller only detects collisions while moving. You could always attach a child collider to the controller and put your collision script on that child. If you're using OnTriggerEnter, be sure one of the objects involved has a rigibody attached.
OFC I was missing the rigid body, ... probably he did too, ...
I actually doubt he was missing the rigidbody, since he's saying "It only works when the character is moving", which means it's detecting the collision when Character is moving. That's why I'm guessing he's using a CharacterController, since that's the only collider type in Unity that detects collision ONLY while moving.
Yes I'm using a CharacterController. I will try it with an normal Collider soon.
Okay with another collider on the player and a rigibody on it it finally works. Thank you