- Home /
Picking up items with Rigidbodies
In my scene I have a Standard Assets' First Person Controller (with a kinematic Rigidbody and a CharacterController component).
At a certain point, I spawn an arbitrary number of pick-up-able objects. These objects need to be affected by physics, and therefore have a (non-kinematic) Rigidbody and a physical BoxCollider. They also have a SphereCollider marked as Trigger that is significantly larger than their BoxCollider, and marks the zone the player has to enter to pick up the object.
In case it's relevant, the objects' Rigidbodies are restricted to only Y movement (rotation is free in all directions), and everything uses Discrete collision detection.
The problem I am facing is that the OnTriggerEnter method in the script attached to of these objects does not get called consistently when my character enters the SphereCollider. In fact it generally only gets called after I've bumped into the object several times with my character.
Now according to the table in the manual, OnTriggerEnter should be getting called in this case:
However it is not working properly.
I have tried a few alternatives, like:
attaching the OnTriggerEnter script to the character instead of the objects
putting one or both of the object's colliders onto (distinct) child objects
removing the SphereCollider entirely and only using the BoxCollider with the OnCollisionEnter method instead of OnTriggerEnter
The first two had the exact same result as the original setup, while the third didn't work at all.
The solution I'm currently using is a raycasting in the character's Update towards all pickup objects (which only have their BoxColliders, no trigger SphereColliders) in the scene to see if they're close enough, and picking up those that get hit by the raycasts. This does work properly, but it seems like a very crude and inelegant solution, and with a massive performance hit in the case of a larger number of pickup objects (calling GameObject.FindGameObjectsByTag every frame, and raycasting towards all of them).
Why isn't the original setup working, and is there a better way to approach this problem?
P.S. In the same scene I use a very similar setup with the tool rack (which spawns the tools when approached), except that it doesn't have a Rigidbody component (as it is stationary and doesn't need to be affected by physics). That one works without problems with a physical BoxCollider and a trigger SphereCollider.
Could you please clarify what type of collider you have on your character? (I'm not very familiar with that asset you mentioned.) I suspect you want to have BOTH a trigger and non-trigger collider on your character, (just like you already do for your spawned objects). When the two trigger-colliders touch, an OnTriggerEnter message should always be called for both (though you don't need to actually handle the event in both).
P.S. rather than use raycasts, since you are trying to simulate sphere colliders, perhaps you can simply compute the distance to the object?
I have a CharacterController component on my character, which serves as a collider.
Fair point about the distance calculation. I went with the raycast approach to prevent being able to pick up objects through the wall (was planning to do that in OnTriggerEnter anyway), but distance calculation would indeed work to simulate SphereColliders. However it still requires a call to GameObject.FindGameObjectsByTag every frame, and iterating through all of them.
Answer by dsmeathers · Aug 07, 2015 at 08:34 PM
Sounds like what your doing should work. If you're using a capsule collider for the player, try changing it to a box. Capsule colliders vs triggers have always been really buggy in Unity.
I've removed the SphereColliders from the objects and ins$$anonymous$$d added a trigger Box Collider to the player object and it works now. Cheers.
Answer by curiouspers · Aug 07, 2015 at 05:16 PM
I think you have unpredictable results because you have two Colliders on object. Putting one of the object's colliders onto (distinct) child objects, AND correcting your code should help. OR maybe this is your solution: here It'll be so much simpler to say if you've attached example scene.
I don't know how to attach a scene, as if I try to edit the question with Attachment (ctrl + U) and add a .unity scene file, it says "This attachment is not permitted because the file type is invalid."
I've seen a similar thread to the one you linked. I've tried putting transform.position = transform.position + Vector3.zero in the tool's Update function, but it didn't help.