- Home /
Trigger doesn't work 100% of the time.
I am having such a hard time with this and I cant figure out why. I have two game objects: "player" and an enemy called "skeleSword". Both have rigid bodies and colliders. Player is using the FPS controller. "skelesword" has two child objects containing colliders: one on the weapon and one on the head. I scripted the weapon collider to destroy "player" on trigger enter. I scripted the Head collider to destroy "skelesword" on trigger enter. Then I made "skelesword" a prefab that spawns at random intervals. The problem I'm having is this: the body collider doesnt always detect a collision and the player falls through it.
Code:
Head Collider:
private GameObject skeleSword;
void Start () {
skeleSword = GameObject.FindWithTag("skeleSword");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player") {
Destroy (skeleSword);
Debug.Log ("enter");
}
}
Weapon Collider:
private GameObject hank;
void Start () {
hank = GameObject.FindWithTag("Player");
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player" ) {
Destroy (hank);
Debug.Log ("enter");
}
}
Here are some images from the project:
Weapon Collider is the exact same as the Head collider, but with the weapon collider script.
What do you mean by this?
the body collider doesnt always detect a collision and the player falls through it.
Is the player jumping on top of the enemy?
yes, the player is jumping up, and falling down through the trigger.
I've noticed that it is more likely to happen if the same prefab is spawned again quickly after the previous one.
Answer by kyrill91 · Feb 12, 2014 at 12:22 AM
I figured it out! Figures... I always figure something out shortly after I resort to unity answers. So the code destroyed the prefab with the "skeleSword" tag. SO, if there was more than one of that particular prefab spawned, then they both had that tag. I changed
Destroy (skeleSword);
to
Destroy (this.gameObject);