- Home /
On Trigger Enter no longer works? long delay to activate
Hello Everyone,
My game is a 3D game. Earlier, I had a basic repair script attached to a gameobject. Whenever I swing my repair hammer, it turns on it's collider and if it something with the tag "repairable" it would then do a "theCollider.transform.SendMessage ("ApplyHeal", TheRepair, SendMessageOptions.DontRequireReceiver);"
I then noticed that my other onTrigger functions are doing the same, such as an arrow or bullet doesn't collide with the wall when it hits, unless I'm very far away. If I get to close, it just passes through the object like it doesn't exist.
It seems to have started since my last Unity update, but I doubt that it the cause.
Would anyone be able to point me in the right direction on what is going on?
I apologize for the vagueness, but I can't really pinpoint what the problem is, aside from the On Trigger is working much slower if at all.
I tried changing the fixedTimeStep and such, but it doesn't seem to help.
Thank you for your time.
Answer by Therian13 · Feb 23, 2015 at 09:07 AM
I finally found out what was causing it. It was REALLY weird, but for some reason my physics manager had it that anything on my draw layer (in this case my arms and hammer) couldn't hit anything layer Default (the repairable objects)
But good news is I got it fixed! Still no idea how that happened. I haven't touch the physics manager in months... 0_o
But thank you everyone for your help! :D
Answer by skylem · Feb 23, 2015 at 06:07 AM
i've experienced similar issues with triggers and colliders sometimes to fix these u can change the rigidbodys collision detection to continuous, if this doesn't help you maybe you change some of your system to be a raycast, as for your trigger delay i can't be sure of the cause but if i were to speculate i would have to wonder how much information your script processes before doing something with the trigger i'll provide a couple of my own methods for using triggers to define an object for interaction and also a simple raycast that i use to loot items which should be easy to adapt for alot of cases such as shooting bullets.
this is my method for assigning an interactable object. u can place this on any object that has a trigger.
void OnTriggerEnter(Collider Other){
if (Other.gameObject.tag == "Player") {
PlayerInteraction pit = Other.GetComponent<PlayerInteraction>();
if(pit.interTarg == null) {
pit.interTarg = gameObject;
}
if(pit.interTarg != null) {
pit.nextTarg = gameObject;
}
}
}
void OnTriggerExit(Collider Other){
if (Other.gameObject.tag == "Player") {
PlayerInteraction pit = Other.GetComponent<PlayerInteraction>();
if(pit.interTarg == gameObject && pit.nextTarg != null) {
pit.interTarg = pit.nextTarg;
pit.nextTarg = null;
}
if(pit.interTarg == gameObject && pit.nextTarg == null) {
pit.interTarg = null;
}
}
}
}
// a brief breakdown of how it works this assumes that the Player has a script called PlayerInteraction upon this PlayerInteraction script there is 2 GameObject Variables one named interTarg, which is the current target for interaction. and nextTarg ,this is for if we manage to stand on 2 triggers simaltanously.
and note that you will never need to make changes to the above
since u will be doing everything with these Interactions within your PlayerInteraction a simple example to be used in playerinteraction below.
// first checking if we have a target of interaction.
if(interTarg != null) {
// this is where would check information regarding the target
// before we do something to it
if(interTarg.tag == "Shop") {
showShopGUI = true;
}
}
// then i would go on to use that bool for some GUI display.
// Forward Raycast from our cam, hit represent the impact point of the ray
// i use it below to see if we hit a transform then check the name of that transform
// before accepting an input key to pick the item up
RaycastHit hit = new RaycastHit ();
/// uncomment below to see the raycast in the scene.
//Debug.DrawRay (GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward * 3f, Color.blue, 1f);
Physics.Raycast (GetComponentInChildren<Camera>().transform.position, GetComponentInChildren<Camera>().transform.forward * 3f, out hit, 15f);
if(hit.transform != null && hit.transform.tag == "Loot") {
if(Input.GetKeyDown(Keycode.E)) {
LootItem(hit.transform);
}
}
// if i was unclear on anything or u need help understanding something i didn't cover feel free to email me Skylem@live.com.au appologies for the long winded answer, i did my best to only include information relevant to a possible solution
the scripts are fairly simple. just your basic onTrigger enter(theCollider : Collider) if (the collider.tag == "something" Send the damage;
The hammer doesn't have a rigidbody on it, however it is parented to the character which does have one. It used to work just fine, but just seemed to stop.
I can't use raycast, because the parts you repair are childen of another gameobject, and the raycast seems to only hit the main parent, regardless of what I target (which in itself is strange).
ah well i can only guess as to what might have caused it to stop working all of a sudden, have u made changes to physics collision/layers since they were working?, as for the raycast hitting parented objects, u can access children objects with the following i don't know if its applicable to your situation but heres the code.
transform.GetChild(0); // 0 represents the first child object
// also u can use
if (transform.childCount > 0)
// to deter$$anonymous$$e wether the object has any children.
//i also use the following if i want to specify a child object to find using its name
// transform.FindChild("9$$anonymous$$$$anonymous$$Pistol");
hopefully this can help.
although your thing wasn't what was wrong with my scene, I went ahead and did an "UP" since you gave some really good feedback.
Thank you! :D
no problem, the amount of ways u can achieve a desired affect are so varied its almost overwhel$$anonymous$$g, i find that sharing different examples is as good a way as any to remember these techniques, even if they aren't always valid to the current issue odds are you'll find them useful in the future, Good luck with your future scripts (y).
Your answer
Follow this Question
Related Questions
Tesla Tower Trap 1 Answer
Time Function in Javascript? 1 Answer
Time Delay Animation 1 Answer
Delay before any action 3 Answers