I need some help with the scripting for my enemies.I want them to explode on collision with the bullet, i have all the explosion and smoke prefabs ready but i need to know how to script the collision.
I’ve tried this script:
function Update ()
{
if(hit.gameObject.tag == “Bullet”)
{
Destroy(gameObject);
}
}
Though i get a error message that says : Unknown identifier: hit.
Anybody knows whats wrong?
where did you get hit from? it isnt defined anywhere.
instead of update you should use collision_enter
check this page:
Ok i looked at the tornadotwins tutorial on youtube.
But i’ll try that one out, Thanks!
I tried it out but you dont seem to understand.
I want it only to explode when colliding with bullets and that script makes it explode when colliding with anything.
Here is how to detect collisions :
//you make a new function with the predefined name OnCollisionEnter
function OnCollisionEnter( hit : Collision ){ // where hit is the collision that occurs
// then put your code here
if(hit.gameObject.tag == "Bullet")
{
Destroy(gameObject);
}
}
also note that collisions dont occur unless atleast one of the two colliding objects has a rigidbody component attached
Ok thanks i will try this one out to.
Thanks it works quite good but now the enemies some times falls half through the terrain.
I think it might be the rigidbody but i dont know how to fix it!
You know whats wrong?