Jump to content

Headshot Kills?


havenphillip
 Share

Recommended Posts

What's the best way to approach headshots? I tried messing with the AI script in the Hurt() function. I tried making a script that created a box around the head. I tried making a pick...seems like the best way would be to add an "if" statement in the AI script Hurt(damage) function by declaring the head but how?

Link to comment
Share on other sites

A cheap way to determine if it is a headshot is by creating a box that covers the head. When you do a succesful pick on the enemy, you get an entity and a pick position back. The entity (enemy) should have a reference to the headBox. Now simply check if the picked position is in the bounds of the entities headbox.

Another basic way is iterating over the enemies bones and see which one is the closest. If the bone has the name 'head' (or whatever it is called), you register it as a headshot. This might give some false positives in some situations depending on the bone structure.

Link to comment
Share on other sites

I tried making a hitbox like from your video, but in your video it's just one box and I can't make a distinction in damage on the limbs. I presume there is something I'm missing or did wrong, but I couldn't get that to work. In the AI script I referenced it in the Start() as:

self.headshotBox=self.entity:FindChild("Headshot Box")

I made a pivot and attached it to the head limb and attached a script that created a box there:

function Script:Start()
    self.head = self.entity:GetParent()
    material = Material:Load("HUD Elements/Materials/_original.mat")

    self.box = Model:Box(0.35,0.35,0.35)
    self.box:SetMaterial(material)
    self.box:SetPosition(self.head:GetPosition(true))
    self.box:SetParent(self.head,true)
    self.box:Translate(1,0,0)
end

 

The box works like this and reduces health with damage but now I don't know how to reference it. I'm assuming I could do something like in the AI script under the Hurt() function add an "if" statement but what? "If target? == self.headshotBox then...?"

Or should I add some kind of Hurt() function to the box itself?

 

 

Link to comment
Share on other sites

Can you show a screen of your enemy in the scene tree? 

When the pick takes place, you get back the entity it picks. Since this is the enemy with (I assume) the AI script, you call its Hurt() function. You can pass along the hitposition with that function. In the hurt function you start looking for bones or boxes that represent body parts like the head. It would be even better if the references to the boxes are there before the Hurt function is called.

  • Enemey (AI script)
    • BodyHead
    • BodyLeftArm
    • etc

 

Link to comment
Share on other sites

  • When you shoot you perform a pick. If it is an enemy the Hurt function is called right?
  • Pass along the pick position with this hurt function.
  • Inside the hurt function, you need to have a reference to this box.
    • You can do this by creating entity properties, or by looping over the entities children until you find the name of your headshot box. (note that when you loop, the pivot might not have been called yet and the box could possibly not exist.)
Link to comment
Share on other sites

Ok sorry I'm lost already. The pick position? Pass the pick and the Hurt() into the Headshot Box script? Actually that part makes sense but where is the pickposition? Are the entity children a table somewhere that I can iterate them? How do I find the exact limb? It's not as simple as:

Hurt()...

if self.headshotBox then

damage = 100

end

 

I can see I should have thought this through longer.

Link to comment
Share on other sites

There is a part in the script, either the player or weapon script, I can’t remember, where an entity is picked and then it calls something like GetTopParent() to return the enemy. The picked entity could be the head of another part of the body so if you can find that bit of code you can modify it to also return the picked entity.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Oh yeah in the gun code. This one?

function Script:FindScriptedParent(entity,func)
    while entity~=nil do
        if entity.script then
            if type(entity.script[func])=="function" then
                --if entity.script.enabled~=false then
                    return entity
                --else
                --    return nil
                --end
            end
        end
        entity = entity:GetParent()
    end
    return nil
end

Link to comment
Share on other sites

Well so I was able to create a script that I can child to the head and drag in the top parent as an entity. I like this because I don't have to do anything to other scripts. It works but the only problem is when I set the material to Invisible.mat I get no bullet wound texture when shooting the head. The invisible texture hides that, too. Is there an easy way to fix that in this script? I can live without it but it irks me a bit.

Here's the script:

Script.parent= "" --entity "Parent"
Script.size = Vec3(0.30,0.34,0.34) -- Vec3 "Size"
Script.pos = Vec3(0,0.1,-0.07) -- Vec3 "Offset"
Script.damageMulti = 10 -- int "Damage Multiplier"

function Script:Start()

    self.head = self.entity:GetParent()
    self.health = self.parent.script.health

    self.box = Model:Sphere(8)
    self.box:SetScale(self.size)
    self.box:SetKeyValue(self.parent:GetKeyValue("type"))
    self.box:SetPosition(self.head:GetPosition(true))
    self.box:SetParent(self.entity,true)
    self.box:SetPickMode(self.parent:GetPickMode())
    self.box:Translate(self.pos)
    self.box:SetShadowMode(0)

    self.material = Material:Load("Materials/Effects/Invisible.mat")
    self.box:SetMaterial(self.material)

end

function Script:Hurt(damage,distributorOfPain)
    if self.health>0 then
        self.health = self.health - (damage * self.damageMulti)
        self.parent.script.health = self.parent.script.health - damage * self.damageMulti
        if self.health<=0 then
            self.health = 0
            self.parent.script.health = Math:Clamp(self.parent.script.health, 0, self.health)
            self.parent.script:SetMode("dying")
            self.box:Hide()
        end
    end
    return
end

 

demo.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...