- Home /
object collision does not work
Hello, i am trying to make a simple tower defence game, and i have some problems with enemy and tower collision..
I have this Enemy script:
void OnCollisionEnter(Collision collision) {
Debug.Log (collision.gameObject.name); // never happens
if(collision.gameObject.name=="col"){
health--;
print(health);
}
}
And theres a box collider on the enemy aswell.
And then i have a tower object, with a child "col" that have a sphere collider.
Some Pics: https://www.dropbox.com/s/awwwtw8h45w2xo5/Screenshot%202014-05-01%2014.07.37.png https://www.dropbox.com/s/grrfnbcbesjsqfc/Screenshot%202014-05-01%2014.08.33.png
What am i doing wrong? Thank you in advance
P.S. also tried to add the sphere collider to the tower object, didn't work.
Answer by dovonobob · May 01, 2014 at 02:52 PM
from the manual:
"Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
does either have a non-kinematic rigidbody?
Also if you're saying the debug.log never occurs then why don't you do a simpler bit of logging so you know 100% whether the collision is occuring or the problem is in one of the other lines in your collision code:
Debug.Log ("ON COLLISION ENTER CALLED");
If that never gets logged then at least you know 100% that function isn't being called and it's a problem in your collider setup.
EDIT: having looked at your screenshots I can't see any rigidbodies attached so that's your problem I reckon.
ha! got to read the manual next time, lol. Thanks a lot! it is working fine now.
also used Sunrise$$anonymous$$ingdom advice to use OnTriggerEnter function. Thank you.
Got to love that manual ;)
No problem bud glad you got it working :)
Answer by SunriseKingdom · May 01, 2014 at 11:35 AM
I would use
function OnTriggerEnter (other : Collider)
{
}
On the enemy Collider make sure you Check on Is Trigger
Hope this help.
I also post videos online link text
well, i changed the code as you said:
void OnTriggerEnter(Collider collision) {
Debug.Log (collision.gameObject.name);
if(collision.gameObject.name=="col"){
health--;
print(health);
}
}
also checked the isTriger box on, but still no progress