Slimwaffle Posted June 14, 2018 Share Posted June 14, 2018 Hi guys. I just started working on a project and I am trying to make an enemy health bar. I attach the script to a pivot and make it a child of the enemy. And it works fine. What I need help with is for my if statement with the and condition. I want to do some kind of check against the FPS script as an extra and condition. To say if monster is within an amount of distance to the cross hair then draw to screen. --Player Enemy Health Bar-- Script.player = nil Script.EnemybarColor = Vec4() --color "Enemybar Color" Script.size = Vec2(0,0) --Vec2 "Size" Script.offset = Vec2(0,0) --Vec2 "Offset" function Script:Start() self.player = self.entity:GetParent() --Staminabar Fractioned local c = self.EnemybarColor self.EnemyColorFractioned = Vec4(c.x/255, c.y/255, c.z/255, c.w/255) end function Script:PostRender(context) if (self.player.script.health < self.player.script.maxhealth) and (self.player.script.health > 0) then --redefine values to make the script cleaner local a = self.player.script.health local b = self.player.script.maxhealth local c = self.EnemybarColor local e = a / b local ox = self.offset.x local oy = self.offset.y local sx = self.size.x local sy = self.size.y local p0 = self.player:GetPosition(true) --Background Bar context:SetBlendMode(1) context:SetColor(1,1,1,.25) context:DrawRect (context:GetWidth() - (context:GetWidth() / 2) + ox, (context:GetHeight() - (context:GetHeight() / 2)) + oy,sx,sy) --Draw the Enemybar context:SetBlendMode(0) context:SetColor(c) context:DrawRect (context:GetWidth() - (context:GetWidth() / 2) + ox, (context:GetHeight() - (context:GetHeight() / 2)) + oy, sx * e, sy) end end -- x axis use -(number) to offset, y axis use a positive number above 0 Quote Link to comment Share on other sites More sharing options...
Slimwaffle Posted June 14, 2018 Author Share Posted June 14, 2018 Another approach I just thought of. Is it possible to draw at monsters position once engaged into combat if I merge this into monster ai script? So that the bar would appear over the monster. I look away and can't see it. And as I look back it stays over the monster. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 14, 2018 Share Posted June 14, 2018 One thing you can do is for every monster: Get position of monster. use Camera:Unproject(monsterPosition) to get the 2d coordinates of the monster's 3d position Do a distance check from the monster2dCoordinates to the crossHaird2dCoordinates. Quote Link to comment Share on other sites More sharing options...
Slimwaffle Posted June 15, 2018 Author Share Posted June 15, 2018 thanks mate. I really appreciate the help. I will let you know how I go. Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted June 16, 2018 Share Posted June 16, 2018 I have a small function which can help you to define if an entity is inside a cone view of another entity. function CheckConeView(axis,entity,target,angle) local z,u if axis == "x" then z = Transform:Point(Vec3(1,0,0),entity,nil) elseif axis == "y" then z = Transform:Point(Vec3(0,1,0),entity,nil) elseif axis == "z" then z = Transform:Point(Vec3(0,0,1),entity,nil) end u = Transform:Point(target:GetPosition(true),nil,entity) z = Transform:Point(z,nil,entity) local targetAngle = Math:ACos(u:Dot(z)/(u:Length()*z:Length())) if targetAngle < angle/2 then return true else return false end end This should be made as global function, then you can use this as below function Script:PostRender() if CheckConeView("z",self.yourPlayerCamera,self.target,90) and self.targetInRange == true then --display health bar code end end hope this make your idea possible Quote Link to comment Share on other sites More sharing options...
Slimwaffle Posted June 16, 2018 Author Share Posted June 16, 2018 I tried this and for the line; u = Transform:Point(target:GetPosition(true),nil,entity) I am getting an error for nil value for target. I think its because of the AI script only defining target once the AI has engaged you. Any suggestions? Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted June 16, 2018 Share Posted June 16, 2018 target in my function should be an entity, dont try it with Vec3() value Quote Link to comment Share on other sites More sharing options...
Slimwaffle Posted June 16, 2018 Author Share Posted June 16, 2018 I am still not getting it for some reason. So I will explain what I did. I put the first piece of code into the Start() function. The second piece of code in my PostRender(context) function. Then I changed the line; u = Transform:Point(target:GetPosition(true),nil,entity) So that it now reads; u = Transform:Point(self.target:GetPosition(true),nil,entity) And I did all this on my Monster AI script. If I where to change target from a Vec3() to an entity. How would I do this? Because everywhere else in the code it is referred to as self.target Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted June 17, 2018 Share Posted June 17, 2018 You can modify the code and make it work I think, because this function was used in my hand-made PlayerScript then I am not sure it will work on default AI / Player scripts of leadwerks. Quote Link to comment Share on other sites More sharing options...
Slimwaffle Posted June 27, 2018 Author Share Posted June 27, 2018 thanks heaps guys I haven't added in distance check yet. But I got this working perfectly with two lines of code. My only question is now how do I get the position of the head instead. Because it uses the feet and prints the bar below the enemy. Or even if I got entity height somehow? these were the two lines I used. local p = self.entity:GetPosition() local y = player.camera:Project(p) Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 27, 2018 Share Posted June 27, 2018 You can look for a child pivot called UI position that your attach via the editor. If you open the model in the model editor, you can see if the model has a head bone in place. If so you can just look for that child entity and gets is position. Another way is just a float value that you add per character which is an offset to the characters position. 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.