hi.
i’m making a zombie survival game and i need some help.
i need to do so that when zombies hit the front side or back side of a car they will die if the car is driving fast enough
i thought about adding a collider on the car so that when it hits a zombie then the zombie dies, but i just can’t get it to work.
please help me with making a script for it.
Use this code in your car which I assume is a rigidbody
var killingSpeed : float; //minimum speed of car to kill a zombie!
function OnCollisionEnter(other : Collision){
if(rigidbody.velocity.magnitide > killingSpeed && other.gameObject.tag == "zombie")
Destroy(other.gameObject); //LINE ABC
}
If you dont want the zombies to simply vanish with the Destroy function, then you can use ragdolls and change the LINE ABC in the above function to this :
other.gameObject.GetComponent(zombieDyingScript).killMe();
And make a javascript for the zombies called zombieDyingScript and use something like this
var deadZombieBody : Transform; //deadZombieBody is a prefab with ragdoll
function killMe(){
Instantiate(deadZombieBody, transform.position, transform.rotation); //instantiate a dead zombie body
Destroy(gameObject); //make the original 'alive' zombie disappear
}
Hope this helps! BTW this code can be improvised, you can add functions that calculate angular velocity of the car in order to kill a zombie that hits the doors of the car and I am not 100% sure if that the best way to instantiate a ragdoll that way but just giving you an idea!
thanks for this. i will test it in some time.
will it be possible to add a script to the zombies that they get hurt by colliding with something to hard. so when you hit one, with a car they will change to rag-doll mode and loose health when it collides with different objects and then when it’s not moving anymore if it then still has some health left it will get up and fight.