Jump to content

Increase speed as health decreases


Recommended Posts

-- In your character's file

local function set_speed(inst)
	local percent = inst.components.health:GetPercent()
    -- This sets your character's speed to default speed * (1 / health percentage)
	inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED * (1 / percent)
end

-- Your character's main function
local function fn()
	inst:ListenForEvent("healthdelta", set_speed)
end

 

Edited by _zwb
fixed math
Link to comment
Share on other sites

--snip--
local function set_speed(inst)
  	--[[ 
  	Use inst.components.health.maxhealth instead of GetMaxwithPenalty would give your character speed boost,
  	if they have penalty and health is at full 
  	--]]
	local health_lost = inst.components.health:GetMaxWithPenalty() - inst.components.health.currenthealth

  	-- Used math.floor so that your character only get speed boost every 10 hp lost, rather than continuous boost
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "whirlwind_speed_mod", 1.25 + 0.1 * math.floor(health_lost/10) )
end

local master_postinit = function(inst)
	-- snip --
	inst:ListenForEvent("healthdelta", set_speed)
	
	inst.OnLoad = onload
	inst.OnNewSpawn = onload
end

return MakePlayerCharacter("whirlwind", prefabs, assets, common_postinit, master_postinit, prefabs)

 

Edited by _zwb
Link to comment
Share on other sites

8 hours ago, AgentX said:

Thanks for all your help, but is there any way for him to take double sanity depletion from items, such as the rods @_zwb

By rods you mean staff like star caller staves? You can do it like this:

Spoiler

function master_postinit(inst)

  inst:AddComponent("staffsanity")
 inst.components.staffsanity:SetMultiplier(2) -- double sanity drain

 

Edited by _zwb
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...