- Home /
Question is off-topic or not relevant
Toggling Is Trigger on box collider causes it to not collide
Hi everyone, I have a script that uses the OnTriggerEnter function.I have the script attached to a cube and set for Is Trigger.But for some reason the collider isn't working properly and objects pass right through it. Any idea what is up?
I was struggling with this for a while and adding another collider wasn't working for me (the character was still falling through). I also tried making one collider smaller and still nothing. I found on this website https://unitygem.wordpress.com/common-unity-gotchas/ the answer is to just use onCollisionEnter if you have a collider, and onTriggerEnter if you don't have a collider. I removed the duplicate collider, unchecked isTrigger, and added the OnCollisionEnter2D function (my game is 2d) and it works perfectly!
Answer by EION Technology · May 11, 2013 at 10:30 PM
Hello. Is Trigger in unity disables the function of the collider and turns it into a passable trigger. (When you think about it, how would OnTriggerEnter / OnTriggerExit work if it didn't allow you to pass through the collider?) The best way to do the Is Trigger on an object is to add another collider component to the GameObject that you want to be an event trigger. So in other words, the object itself will still have the collider that stops the player, and the Is Trigger collider component will act as the trigger for your GameObject. Make sure that you have the Trigger Collider a little larger than the GameObject itself. Hope this helps.
Lifesaver. I've wasted several hours trying to solve this issue. The solution, as ever, is simple.
Answer by Dreamora · May 11, 2013 at 09:28 PM
If you set it as a trigger, then it will no longer do collisions. It will only trigger the trigger events (enter, stay, leave) This is meant to be like this as you use triggers for cases where you want to detect presence of objects in an area. if you want a physical reaction, then do not mark the object as a trigger but leave it as a collider and it will work just fine.
I have my cube with Is Trigger set to false but my script isn't working. Do I need to change my script so it's specifically for character controllers?
using UnityEngine;
using System.Collections;
public class PlayerDetect : $$anonymous$$onoBehaviour {
public bool somebool;
void OnTriggerEnter (Collider Player) {
if(Player.gameObject.tag=="Player"){
somebool = true;
}
}
}
I edited my script so it would work for character controllers specifically.But now I can't get the script to detect when the player collides with the cube. Any idea why it's not detecting the player?
using UnityEngine;
using System.Collections;
public class PlayerDetect : $$anonymous$$onoBehaviour {
void OnControllerColliderHit(ControllerColliderHit playerCollider){
if(playerCollider.gameObject.tag == "Player"){
Debug.Log("Hit Player");
}
}
}
This function will not work with triggers as CharacterController is no rigidbody and operates partially outside the physics environment I fear
This awesome video shows cases for which collision may not occur...