havenphillip Posted August 17, 2018 Share Posted August 17, 2018 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? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 17, 2018 Share Posted August 17, 2018 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. Quote Link to comment Share on other sites More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 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? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 17, 2018 Share Posted August 17, 2018 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 Quote Link to comment Share on other sites More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 You're saying I can reference the Hurt() function in the Headshot Box script? Quote Link to comment Share on other sites More sharing options...
Josh Posted August 17, 2018 Share Posted August 17, 2018 You should be able to modify the script and get the exact limb that is hit. Quote 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 More sharing options...
AggrorJorn Posted August 17, 2018 Share Posted August 17, 2018 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.) Quote Link to comment Share on other sites More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted August 17, 2018 Share Posted August 17, 2018 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. Quote 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 More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 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 Quote Link to comment Share on other sites More sharing options...
Josh Posted August 17, 2018 Share Posted August 17, 2018 Yes. Find where this function is called and you can get the picked entity. Quote 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 More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 Ah ok. So this right here: function Script:UpdateWorld()... local enemy = self:FindScriptedParent(pickinfo.entity,"Hurt") Ok so now how do I actually get that? You're talking about modifying the gun script? Quote Link to comment Share on other sites More sharing options...
Josh Posted August 17, 2018 Share Posted August 17, 2018 If pickinfo.entity is the characters head it’s a headshot. Quote 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 More sharing options...
Josh Posted August 17, 2018 Share Posted August 17, 2018 Maybe check the entity name and increase the damage if it is “head” or whatever. Quote 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 More sharing options...
havenphillip Posted August 17, 2018 Author Share Posted August 17, 2018 Ok I'll try it. Thanks guys. Quote Link to comment Share on other sites More sharing options...
havenphillip Posted August 20, 2018 Author Share Posted August 20, 2018 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.