- Home /
OnTriggerStay method always choosing the "Other" I don't need
it's not going to be easy to explain my problem so I will do my best:
I've used a Capsule collider attached to my FPS player, it's a trigger that helping my Character to react to it's front environment.
I've tried to use the method "OnTriggerStay" (in a script attached to the capsule collider) for everything that stands infront of him (you can see the picture I've added, this is in_game pause).
this is the script for my method:
void OnTriggerStay(Collider Other){
shorlack = Other.gameObject;
if(Input.GetButtonDown("fus") && Other.tag == "fusrodah"){
fusRoDah (Other.gameObject);
}
}
I've added "shorlack" as a public variable so I know what exactly "Other" is in game time. the problem is, Other is almost always my FPS, only when I appsolutley touches one of the crates, Other referes to it. all the crates are tagged as "fusrodah".
Is there any way to limit my OnTrigger method to "fusrodah" tagged game objects only? and how can I make the method to refer all the crates at once?
Answer by chelnok · Aug 20, 2013 at 07:17 PM
Variable shorlack gets the gameobject always because you have it in OnTriggerStay without any condition. Cant really be sure, but i think you are trying to get this:
void OnTriggerStay(Collider Other){
if(Other.gameObject.tag == "fusrodah"){
shorlack = Other.gameObject;
if(Input.GetButtonDown("fus")){
fusRoDah (Other.gameObject);
}
}
}
To ignore your player you could use tag for player (and all childs)
if (other.tag == "player") return;
Better way is assign your player to its own layer, and ignore that layer:
if (other.gameObject.layer == 7) return; //or what ever your layer for player is
Or you can ignore it with collision matrix: http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
Thank you, appearently every I needed was this line: if (other.tag == "player") return;
Well that's just about a million times easier than what I was explaining...
Also +1 for collision matrix ignoring. $$anonymous$$uch better than tags (I never use them) or ignoring manually with code.
Answer by OP_toss · Aug 19, 2013 at 06:08 PM
I recommend using OnTriggerEnter and OnTriggerExit and flipping a bool instead. I trust Update and those methods much more than OnTriggerStay (for whatever reason).
Below is copy-pasted code from another thread talking about this issue (I'm lazy). Try this, it should work better.
var isInTrigger: boolean = false;
function Update(){
if(isInTrigger){
//Do whatever it is you want to do here
}
}
function OnTriggerEnter(){
isInTrigger = true;
}
function OnTriggerExit(){
isInTrigger= false;
}
I don't use OnTriggerStay because it seems to depend too much on Unity to do work that I could easily do myself. It is also another method that potentially could run every update. I like to keep all repeating actions in one place. I probably want to know if it is in the trigger or not anyways, so keeping track of the bool isn't asking too much. And lastly, I think querying input inside a specialized method like that could potentially not work. I believe it's recommended to use Update for input checks.
Hope this helps!
this way I should put the script on the objects, I want the script to be only on my FPS. before OnTriggerStay I tried OnTriggerEnter, it doesn't work for me either
Do you have a RigidBody attached to your FPS? Are your colliders on your crates marked as Triggers? Are you definitely getting an enter/stay event to fire?
my FPS is doesn't have rigidbody attached, the colliders on my crates aren't triggers. I even tried with both enter and stay, they both give me the same reaction, I have to touch the crate with my FPS
If your FPS moves, and it has a collider, it needs a rigid body in order to calculate properly. All moving colliders need rigid bodies.
If you want your crates to react and send the OnTrigger_ messages, you need them to be Triggers. Thus OnTRIGGER.
I'm surprised you're getting anything without the above setup...
when my crates are triggers, they just falling off the ground. there is no difference when my FPS is rigidbody for me