• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Raising/Lowering heights with a timer

Level 29
Joined
Jul 29, 2007
Messages
5,174
I made this nice and easy code that raises or lowers units based on a timer.
You set the height which the unit will stop moving/die (you choose if he dies based on a boolean) and what speed he moves at.

Here is the code with more explenations inside it

JASS:
//****************************************************************************************
//               Move unit's height with timers
// call goingdown(whichUnit,speed,deadheight,kill)
//
// Warning ! The dead height CANNOT BE 0 ! (I think this is a bug in warcraft)
// If you want your units to die/stop on the ground, make the deadheight=1
//
// If you want the unit to go up, its height should be -lower- then the deadheight and vice versa.
//
// Remember: to calculate speed per second, do your speed value * 50 !


function goingdowntimer takes nothing returns nothing
       local timer t = GetExpiredTimer()
       local boolean kill = GetHandleBoolean(t, "kill")
       local real deadheight = GetHandleReal(t, "deadheight")
       local unit whichUnit = GetHandleUnit(t, "whichUnit")
       local real speed = GetHandleReal(t, "speed")
       local real whichUnitStartheight = GetHandleReal(t, "whichUnitStartheight") 
       local real whichUnitHeight = GetUnitFlyHeight(whichUnit)
       if deadheight > whichUnitStartheight then
            if whichUnitHeight < deadheight then
                call SetUnitFlyHeight(whichUnit, whichUnitHeight+speed, 1000000)
            else
                call FlushHandleLocals(t)
                call DestroyTimer(t)
                if kill == true then
                    call KillUnit(whichUnit)
                endif
            endif
       elseif deadheight < whichUnitStartheight then
            if whichUnitHeight > deadheight then
                call SetUnitFlyHeight(whichUnit, whichUnitHeight-speed, 1000000)
            else
                call FlushHandleLocals(t)
                call DestroyTimer(t)
                call KillUnit(whichUnit) 
            endif
       endif  
       set t = null
       set whichUnit = null
endfunction
function goingdown takes unit whichUnit, real speed, real deadheight, boolean kill returns nothing
       local timer t = CreateTimer()
       local real whichUnitStartheight = GetUnitFlyHeight(whichUnit)
       call SetHandleBoolean(t, "kill", kill)
       call SetHandleReal(t, "whichUnitStartheight", whichUnitStartheight) 
       call SetHandleReal(t, "deadheight", deadheight) 
       call SetHandleHandle(t, "whichUnit", whichUnit)
       call SetHandleReal(t, "speed", speed)
       call TimerStart(t, 0.02, true, function goingdowntimer)
       set t = null
endfunction


Example raising units and killing them when they get to the max height you want

JASS:
function Example_Actions takes nothing returns nothing
    local unit whichUnit = GetTriggerUnit()
    call goingdown(whichUnit,10,800,true) // WhichUnit will raise at 500 per second untill 
                                          // he gets to a height of 800 and then he will die    
endfunction

//==== Init Trigger NewTrigger ====
function InitTrig_Example takes nothing returns nothing
    set gg_trg_Example = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Example, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction(gg_trg_Example, function Example_Actions)
endfunction


Example lowering units and not killingthem when they get to the max (min) height you want

JASS:
function Example_Actions takes nothing returns nothing
    local unit whichUnit = GetTriggerUnit()
    call goingdown(whichUnit,5,1,false) // WhichUnit will get lowerd at 250 per second untill 
                                          // he gets to a height of 1   
endfunction

//==== Init Trigger NewTrigger ====
function InitTrig_Example takes nothing returns nothing
    set gg_trg_Example = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Example, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddAction(gg_trg_Example, function Example_Actions)
endfunction


I will say it again, please notice that the lowering/raising is based on the unit's current height which means that if you give him a value higher then his height - he will be flying up, if you give him a lower value then his height, he will descend down.
 
Top