- Home /
OnCollisionEnter not entered on obvious collision
Hei there
I'm quite new to Unity, and I'm having a problem. I'm spawning an empty casing prefab as part of the fireing of a gun, imparting it with some velocity and letting it fly. I then want to detect when it hits the ground and play a sound and then remove it. But the collision even never fires.
The casing has a capsule collider and a non-kinematic rigidbody on it, and I've double checked that the ground has a (non-trigger) mesh collider on it. The rigidbody starts out being a trigger, but is changed to not be a trigger soon after it's spawned in. When I play the game I can see the casing being spawned and hitting the floor (where it bounces around a bit), but OnCollisionEnter never get's called. Below is the script I've attached to the casing prefab.
using UnityEngine;
using System.Collections;
public class CasingScript : MonoBehaviour {
public float timeImmunity;
public float timeMax;
float timeCreation;
bool collided = false;
void Awake() {
timeCreation = Time.time;
}
void FixedUpdate() {
if ((Time.time - timeCreation) > timeImmunity) {
collider.isTrigger = false;
}
if (collided && !audio.isPlaying) {
Debug.Log("Casing destoryed");
Destroy(gameObject);
}
if ((Time.time - timeCreation) > timeMax) {
Debug.Log("Casing timed out");
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision other) {
if ((Time.time - timeCreation) < timeImmunity) {
Debug.Log("Casing collided; immune");
return;
}
if (!collided) {
collided = true;
audio.Play();
Debug.Log("Casing collided");
}
}
void OnTriggerEnter(Collider other) {
Debug.Log ("Triggered against " + other.gameObject.name);
}
}
hmm, to much code for me to stomach right now, but whenever you want to do collisions, my rule is make sure you have colliders and rigidbodies on both objects, collision can be unpredictable otherwise. well predictable, but tricky. just test it real quick with a rigidbody on the ground with the gravity component unchecked.
I did check that. It still didn't detect the collision, but it did cause the ground to move undesirably.
Did you Checked the collider.isTrigger is set to false at runtime? Because maybe the condition is not true and the isTriggered option keeps being false.
$$anonymous$$aybe add a Debug line to test if condition is being done
I added a OnTriggerEnter with a debug statement, it's not being called either.
Answer by khh · Feb 22, 2014 at 12:33 AM
Seems to be a bug with the capsule collider. I changed the collider to a boxcollider without changing anything else, and then it suddenly started working as expected.
Answer by Maximillion · Feb 21, 2014 at 10:06 PM
Have you directly added in a Debug for the OnCollisionEnter?
void OnCollisionEnter(Collision other) {
if ((Time.time - timeCreation) < timeImmunity) {
return;
}
collided = true;
audio.Play();
Debug.Log("Bullet Hit");
}
If that is shown, there is a possibility that the " && !audio.isPlaying" is causing the issue. But let me know whether you have and give it a go if not.
I tested that, and updated the code. OnCollisionEnter isn't being called at all, even though I can see the object hitting the ground.