head shots

i’m trying to script damage to enemy. this enemy has a capsule collider up to his neck, and then i have a sphere collider covering his head. i’ve tried several different ways, but none seem to work.everything works up until the headshot. i tagged the sphere as a HeadCollider. if there are any suggestions or others ways to do it please let me know.

#pragma strict

var range: float = 1000;
var force: float = 1000;
var clip : int   = 20;
var bulletsPerClip:int  = 60;
var reloadTime :float  = 3.3;
var bulletsLeft: int = 0;
var shootTimer: float = 0;
var shootCooler: float = 0.9;
var shootAudio: AudioClip;
var reloadAudio: AudioClip;
var damage: int = 10;  
var instantDeath: int = 100;





function Start () 
{

	bulletsLeft = bulletsPerClip;


}

function Update () 
{
	if(shootTimer > 0)
	{
		shootTimer -= Time.deltaTime;
	}
	
	if(shootTimer < 0)
	{
		shootTimer = 0;
	}
	
	if(Input.GetMouseButton(0) && bulletsLeft)
	{
		if(shootTimer == 0)
		{
			
		
		PlayShootAudio();
		RayShoot();
		
		shootTimer = shootCooler;
		
		}
	}
}

function RayShoot()
{
	var hit: RaycastHit;
	var directionRay = transform.TransformDirection(Vector3.forward);
	Debug.DrawRay(transform.position, directionRay * range, Color.red);
	
	
	if(Physics.Raycast(transform.position, directionRay, hit, range))
	{
		if(hit.rigidbody)
		{
			hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
			hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
		}
		else if(hit.collider == GameObject.FindWithTag ("HeadCollider"))
				{
					 Destroy(gameObject);
					//hit.collider.SendMessageUpwards("ApplyDamage", instantDeath, SendMessageOptions.DontRequireReceiver);
				}
		
	}
	
//	if(Physics.Raycast(transform.position, directionRay, hit, range))
//	{
//		
//	}
	
	
	bulletsLeft --;
	if(bulletsLeft < 0)
	{
		bulletsLeft = 0;
		clip --;
	}
		
		if(bulletsLeft == 0)
		{
			Reload();
		}
	
	
}

function Reload()
{
	PlayReloadAudio();
	yield WaitForSeconds(reloadTime);
	
	if(clip >0)
	{
		bulletsLeft = bulletsPerClip;
	}


}

function PlayShootAudio()
{
	audio.PlayOneShot(shootAudio);

}


function PlayReloadAudio()
{
	audio.PlayOneShot(reloadAudio);

}

On line 69 in your code you have:

 else if(hit.collider == GameObject.FindWithTag ("HeadCollider"))

I tested this and the if statement will never return true since the two arguments will never be equal… However if you make sure they are of the same type e.g. a string then it does work.
Here is an example:

else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)

This worked for me so I hope you will be able to achieve the same results :slight_smile: The statement is basically checking if the gameobject that was hit has the same name as the object that was tagged with the “HeadCollider” tag.