- Home /
OnCollision Enter stopped working Please help
Hi , I had a script working fine for shooting rockets at an enemy and doing damage based on the enemy. For some reason now though it has stopped working and i cant figure out why
Rocket prefab has rigid body and mesh collider Enemy has Rigidbody and character controller
here is the code i have...print isnt triggering
var explosion : GameObject;
function OnCollisionEnter(myCol: Collision)
{
var contact : ContactPoint = myCol.contacts[0];
var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal ); // Ensure Explosion follow science!
var instantiatedExplosion : GameObject = Instantiate(explosion, contact.point, rotation );
print("Hit something");
if(myCol.gameObject.name == "Enemy")
{
myCol.gameObject.SendMessage("ApplyDamage", 10, SendMessageOptions.DontRequireReceiver);
}
Destroy(gameObject);
}
Any help greatly appreciated
You probably have disabled or lost the collision volume associated with the object.
GameObjects need an attached physics object to receive the OnCollision() calls.
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
You'll want to make sure the collider is active for the Rocket and the Enemy so that they can actually collide.
everything appears active on the rocket and the enemy =/
Rocket has mesh collider and enemy has character controller
Answer by fafase · Apr 22, 2012 at 09:50 AM
Would it have stopped working after you added or altered the CC?
Try to set the rigidbody of the enemy to IsKinematic.
That will disable the collision of the rigidbody. At the moment I would think you have collision detected for the CC and the rigidbody.
I had a similar answer 3 days ago and I fixed the issue as such:
"I made a simplified version, I have a sphere which act as your bullet, it has rigidbody, sphere collider (no IsTrigger) and just a movement script. Then i have another sphere that acts as your enemy with CC, rigidbody (IsKinematic) and a sphere collider (no IsTrigger).
It has only this script:
var lives = 5;
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag =="Sphere")
Debug.Log("Collision");
lives-=1;}
And it works. I have not altered any other members of it all, just add components and that is it. MAybe you could add a collider to your enemy. As you rigidbody is set to IsKinematic, it won't interfere as IsKinematic disables the collider."
here is the original question http://answers.unity3d.com/questions/241963/enemy-not-getting-hit-collider-issue.html
Answer by BenJohnston · Apr 22, 2012 at 03:47 PM
Even removing the CC and adding a Sphere collider its the same issue.
I tried adding IsKinematic but still no love
You could try to remove the prefab and place it agina, maybe something went broken at some point and your prefab lost a connection somewhere. By doing it again, if your original prefab is fine, it should work again.
Your answer
Follow this Question
Related Questions
How Can One Collider Recognize Contact With Another? 0 Answers
Damage with collision force? 0 Answers
Base mesh deformation into secondary mesh with collisions. 1 Answer
Car Collision Damage 1 Answer
Cannot detection collision 1 Answer