- Home /
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);
}
I could not get that to work for me. I worked on it during some spare time, and eventually thought to Debug it, so i took out the Destroy(gameObject); and inserted a Debug.Log("Shot to the Head"); --- when i tested it, it recognized the hit, but only one time.. it would never come again..... now i do have a cross hair script that i used the same Debug on and it recorded the hit every time without fail. so...I don't know why . i will continue to research it, but if you have any suggestions that would be wonderful
Answer by ByteSheep · Feb 19, 2013 at 02:18 AM
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 :) 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.
after going back and forth with different ideas, i came back to this one, and i got it to work. here is the code
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 = 50;
function Start ()
{
bulletsLeft = bulletsPerClip;
}
function Update ()
{
if(shootTimer > 0)
{
shootTimer -= Time.deltaTime;
}
if(shootTimer < 0)
{
shootTimer = 0;
}
if(Input.Get$$anonymous$$ouseButton(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.collider.gameObject.name == GameObject.FindWithTag("Body").gameObject.name)
//if(gameObject.FindWithTag("Body"))
{
Debug.Log("I hit the body");
//hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
//hit.collider.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
//else if(gameObject.CompareTag("HeadCollider"))
else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
{
Debug.Log("That was a headshot");
}
}
// var hit: RaycastHit;
// //var directionRay = transform.forward;
// var directionRay = transform.TransformDirection(Vector3.forward);
// Debug.DrawRay(transform.position, directionRay * range, Color.red);
//
// if(Physics.Raycast(transform.position, directionRay, hit, range))
// {
// if(gameObject == gameObject.FindWithTag("Body"))
// {
// //hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
// //hit.collider.Send$$anonymous$$essageUpwards("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver);
// Debug.Log("bodyshot");
// }
// else if(gameObject == gameObject.FindWithTag("HeadCollider"))
// {
// Debug.Log("That was a headshot");
// }
//}
// if(Physics.Raycast(transform.position, directionRay, hit, range))
// {
// if(hit.collider == gameObject.FindWithTag("HeadCollider"))
// {
// Debug.Log("That was a headshot");
// }
// }
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613092939im_/https://answers.unity.com/themes/thub/images/avi.jpg)