Jump to content

Making creature flee when low health


Recommended Posts

Hi there.

I've been working on a creature mod, using the beefalo code as a base. I'm still learning so any help on this would be really appreciated.

So I have been trying to make the creature flee when health is below 20%. I tried adding the RunAway behaviour to the Brain PriorityNode:

	    WhileNode(function() return self.inst.components.health:GetPercent() < 0.20 end,
		    RunAway(self.inst, ShouldRunAway, RUN_AWAY_DIST, STOP_RUN_AWAY_DIST)),

(I wanted the creature to run away from the attacker not just any character/player so the code for ShouldRunAway isn't ideal.)

local function ShouldRunAway(guy)
    return guy:HasTag("character") and not guy:HasTag("notarget")
end

My problem was that when the player attacks the creature, the creature fights back as intended but when health gets below 20% it stops attacking and stands there playing idle animations, when health has regenerated above 20% it comes back to continue fighting.

So on the creature prefab I added the code below for the OnAttacked function:

	if inst.components.combat:HasTarget() and inst.components.health:GetPercent() < 0.20 then
          --inst.components.combat:GiveUp()
	      inst.components.combat:DropTarget()
	end

Now the creature doesn't come back to continue fighting however when health is below 20% it still does not run away. I'm also not sure if I should be adding something to the brain or prefab script to achieve what I want.

Mostly just been guessing and learning through trial and error so any insight into what I can do here will help! Ty for reading :D

 

Link to comment
Share on other sites

Hmm, I had kept my distance and stop run away distance same to what the koalefant uses. However I found a similar code in the Bunnyman brain file and it seems to be working correctly now!

            WhileNode(function() return self.inst.components.health:GetPercent() < TUNING.BUNNYMAN_PANIC_THRESH end, "LowHealth",
                ChattyNode(self.inst, "RABBIT_RETREAT",
                    RunAway(self.inst, "scarytoprey", SEE_PLAYER_DIST, STOP_RUN_DIST))),

edit: also related code on the bunnyman prefab

inst.components.combat.panic_thresh = TUNING.BUNNYMAN_PANIC_THRESH

 

Edited by maliblues
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...