Jump to content

Recommended Posts

Posted

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

 

Posted

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.

Posted

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. 
Posted

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 :)

Posted

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?

Posted

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

  • 2 weeks later...
Posted

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)

E1.jpg

Posted
  1. You can look for a child pivot called UI position that your attach via the editor.
  2. 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.
  3. Another way is just a float value that you add per character which is an offset to the characters position.

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.

×
×
  • Create New...