- Home /
Collider not triggering
I've taken the code from the Lerpz tutorial to make enemies drop pickups when they die. Currently, the enemy dies and drops the pickups yet I can't seem to get them to trigger the collider.
Each pickup prefab has a sphere collider attached and my first person controller does not seem to trigger the collider when passing over the prefabs.
Now in the tutorial there is no RigidBody used for either the pickup prefabs or the player so I haven't used them either yet I have seen in the forums that RigidBodies might need to be attached? If so, in what way? Do both items (pickup and player) need RigidBodies attached or just one?
I have my pickup script attached to the pickup prefab and have OnTriggerEnter(Collider col) declared. With no RigidBodies this is never triggered yet when I add a RigidBody to one of the objects I get null pointer execeptions in OnTriggerEnter, usually because I'm trying to access the Player state script from the Collider passed into OnTriggerEnter and this turns out to be null.
Any help much appreciated!
So OnTriggerEnter and OnCollisionEnter are not triggering! I've got a sphere collider on the pickup and I attached a rigidbody to the character controller with is$$anonymous$$inematic = true.
What I don't understand is that in the 3rd Person tutorial (Lerpz) the OnTriggerEnter works fine! I've copied that code and the only difference is I'm using a first person controller (i.e. no character model) yet it doesn't work.
Answer by peacemaker · Oct 15, 2010 at 10:33 AM
I've fixed this issue by moving the trigger checking to the character controller script rather than on the pickup object. I'm not 100% sure why this should make a difference but as it's working I will go with it for now! :)
So: Controller has OnTriggerEnter defined, Pickups have a sphere collider with isTrigger set to true. When controllers OnTriggerEnter is called I check the tag of the object the controller is colliding with, if it is a pickup object we get the Pickup type from the script attached to the pickup object then remove the object from the world.
having a similar issue. my prefabs do not collide with the player. both the player and prefabs(enemies) are moving but when they collide the OnTrigger function is not called. both have rigid bodies,isTrigger enabled. Any suggestions?
Answer by YetAnotherUnityQuack · Oct 14, 2010 at 10:18 AM
The object moving should have a RigidBody attached; if you don't wish to use physics for that object you could set the "kinematic" toggle as true. Objects without rigid bodies does not check collisions with other non-rigid body colliders.
So in the context of my problem you're saying my first person controller should have a RigidBody attached with kinematic set to true? I was under the impression that the Character Controller component did something similar "under the hood" or is this incorrect?
Thanks for the answer!
It seems that the character controller has its own collision callback function that you should use ins$$anonymous$$d of OnTriggerEnter when doing collision detection with the character. http://unity3d.com/support/documentation/ScriptReference/CharacterController.OnControllerColliderHit.html
Oh, thanks for that! How strange, it seems to work using OnTriggerEnter in the Lerpz tutorial on the Unity website... I mostly copied this system to get $$anonymous$$e in place. I'll give OnControllerColliderHit a try then!
Here is a topic on the subject http://forum.unity3d.com/threads/4016-Collision-checking-without-the-need-of-a-rigidbody
Answer by Janibasha · May 29, 2017 at 03:08 PM
Yes, "isTrigger" should be checked, it works for me too. thanks