- Home /
OnControllerColliderHit troubles with picking up objects.
Hi there,
I'm having some trouble with OnControllerColliderHit and was hoping to get some input on it.
Here's the script I have right now:
// Pickup ammo
void OnControllerColliderHit (ControllerColliderHit hit) {
// When we hit a bullet on the ground, tagged with `pickUp_bullet`
if (hit.collider.gameObject.tag == "pickUp_bullet") {
// We destroy it
Destroy(hit.gameObject);
// And add it back to our ammo count
clipSize++;
countAmmo();
}
}
The idea behind this is to pick up bullets on the ground and add them to your ammo count. The problem I'm encountering is that while this works as it should, I've discovered a way to abuse this script to get more bullets than you should. I can only imagine its the way the ChatacterController collider and the object I'm picking up are interacting. Sometimes, if the prefab is on the ground waiting to be picked up, it can be counted twice.
The bullet prefabs have tightly fitting capsule colliders.
Would it make sense to move this into the void Update () {} so that it's checked once a frame?
It's worth noting, that these bullets cannot be marked as triggers, as they are instantiated prefabs fired from a gun.
Your answer