Trying to use headshots on my zombies

Hello,

I’m trying to put together a zombie game where I must headshot the zombies to kill them. I have an animated character mesh with attached character controller component. On a prototype, I had the zombie as a head and body object where the head could be shot and send a kill message to the body. On this version, however, if I try to add another collider near the head the zombie starts looking as though it is breakdancing.

My question is, is there any way to have the zombie damage script locate the position of raycast hits along the zombie’s character controller so I can simulate a “headshot”? Here is my script:

var hitPoints = 100.0;
var damage = 5.0; 
var damageHeight = 3.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody; 
var deathsound : AudioClip; 

function Headshot (Raycast : Collision) {
	
	var hit : RaycastHit;
	
	if (Physics.Raycast (hit.point.position.y >= damageHeight)) {
		SendMessageUpwards("ZombieDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
}

function ZombieDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
		return;
		
	hitPoints -= damage;
	if (hitPoints <= 0.0) {
		// Start emitting particles
		var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
		if (emitter)
			emitter.emit = true;

		Invoke("DelayedDetonate", detonationDelay);
	}
}

function DelayedDetonate () {
	BroadcastMessage ("Detonate");
}

function Detonate () {
	// Destroy ourselves
	Destroy(gameObject);

	// Create the explosion
	if (explosion)
		Instantiate (explosion, transform.position, transform.rotation);

	// If we have a dead barrel then replace ourselves with it!
	if (deadReplacement) {
		var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

		// For better effect we assign the same velocity to the exploded barrel
		dead.rigidbody.velocity = rigidbody.velocity;
		dead.angularVelocity = rigidbody.angularVelocity;
	}
	
	// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
	// right away
	var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
	if (emitter) {
		emitter.emit = false;
		emitter.transform.parent = null;
	}
}

Keep in mind that I am not myself a coder but lean more towards the artist side. Please respond with specific code examples if possible. Thanks :slight_smile:

I would have a collider for his head separate. Ignore the collisions between the head and the rest of the colliders, because thats probably why it looked like it was dancing. Then in the raycast you can check the hit collider and see if it was a head.

How do I have it ignore the collisions? The collider also seems as though it’s falling to the ground and bringing the zombie with it. Like it fell on its face.

So the collider on the head falls to the ground, or the head falls to the ground?

The collider on the head falls to the ground, but it is a child of the zombie object located near the zombie’s head so the zombie goes with it.

you should add the collider to the actual zombies head

It’s all one mesh. Should it not be? When I try to separate them as two meshes and the head disappears in Unity.

I like it better having separate peices. Gives you more freedom in Unity that way. If the heads disappearing that way you must be exporting it wrong. I dont know anything when it comes to modelling programs though.

I figured it out. I had to put a new instance of the model into the scene. Now my problem seems to be that once the head explodes, I can’t get the body to die with the old scripts.

I had a blood splatter effect that caused damage to the body to make it also die, but that’s not working anymore. Likewise, when I try to send kill messages to the overall zombie object it doesn’t seem to work. Any suggestions?

I would just make a reference to the body script. Then when the head explodes call a method in the body script which makes it die.

Got it to work. Thanks!

no prob!