- Home /
Having trouble making headshots do more damage (JS)
So I have a zombie, and it is the parent of a head model, just the head is tagged "Head", why doesnt this work? shooting the head should do more damage? Thanks (javascript):
var DamageAmount : int = 5;
var DamageAmountHead : int = 15;
var TargetDistance : float;
var AllowedRange : float = 400;
function Update () {
if (GlobalAmmo.LoadedAmmo >= 1) {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance <= AllowedRange && (Shot.collider.tag.Equals("Head"))) {
Shot.transform.SendMessage("DeductPoints", DamageAmountHead, SendMessageOptions.DontRequireReceiver);
}
else if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductPoints", DamageAmount, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
Answer by cstooch · Jun 26, 2017 at 04:49 AM
Maybe do a couple Debug.Log statements..
print out the collider tag right after your if (Physics.Raycast, etc... to make sure it is "Head" when you hit the head..
put one Debug.Log in each branch of the if/else if section below to see which branch gets called when you hit the head.
The code looks like it should work, but the debugging I mentioned might help point out something obvious you are missing.
Assets/Scripts/HandGunDamage.js(13,23): BCE0034: Expressions in statements must only be executed for their side-effects.
I get that error when adding Debug.Log; to the code? Here
var DamageAmount : int = 5;
var DamageAmountHead : int = 15;
var TargetDistance : float;
var AllowedRange : float = 400;
function Update () {
if (GlobalAmmo.LoadedAmmo >= 1) {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
Debug.Log;
if (TargetDistance <= AllowedRange && (Shot.collider.tag.Equals("Head"))) {
Shot.transform.Send$$anonymous$$essage("DeductPoints", DamageAmountHead, Send$$anonymous$$essageOptions.DontRequireReceiver);
Debug.Log;
}
else if (TargetDistance < AllowedRange) {
Shot.transform.Send$$anonymous$$essage("DeductPoints", DamageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
Debug.Log;
}
}
}
}
}
And the head has a collider that is fit to it and the rest of the zombie does too.
Debug.Log is meant for you to print out a message to the console.. i.e. you have to supply it a message, not just say "Debug.Log;"
ex.:
if blah blah blah...
Debug.Log("Head should be hit here");
else if blah blah blah...
Debug.Log("Body should be hit here");
After that's there, and if it works, you would look at the console window in the Unity editor for the debug log messages.
Your answer
Follow this Question
Related Questions
Can't Instantiate the GameObject and make it parent to GameObject which it hits. 0 Answers
UnityException: Tag: GlassCup is not defined. 1 Answer
How to make ray hitting only game objects with tag from list 2 Answers
Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers