• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Angle Between Co-ords?

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
I am trying to make a spell which involves the hero charging towards the targeted unit. However, I cannot seem to get the angle of charging correct. I have used the function:
JASS:
set D.angle = Atan2(GetUnitX(D.caster) - GetUnitX(D.target), GetUnitY(D.caster) - GetUnitY(D.target))
However, this is not very accurate. Any other more efficient way of determining the angle between the co-ords?

Thanks!

EDIT: Also, how would I pause the unit, but still constantly play its "walk" animation while charging?
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Yes. Would you mind taking a look at my code?

JASS:
scope Forward

    private struct ForwardData
        unit caster
        unit target
        real TotalDist
        real DistLeft
        real angle
    
        static method Timer_Actions takes nothing returns nothing
            local timer t = GetExpiredTimer()
            local thistype D = GetTimerData(t)
            local real x
            local real y
            local real x2
            local real y2
            if D.DistLeft > 0 then
                set x = GetUnitX(D.caster)
                set y = GetUnitY(D.caster)
                set x2 = x + 5 * Cos(D.angle * bj_DEGTORAD)
                set y2 = y + 5 * Cos(D.angle * bj_DEGTORAD)
                call SetUnitX(D.caster, x2)
                call SetUnitY(D.caster, y2)
                set D.DistLeft = D.DistLeft - 5
            else
                call ReleaseTimer(t)
                call SetUnitPathing(D.caster, true)
                call PauseUnit(D.caster, false)
                call SetUnitAnimation(D.caster, "stand")
                call D.deallocate()
            endif
            set t = null
        endmethod
    
        static method create takes nothing returns thistype
            local thistype D = thistype.allocate()
            local real xdist
            local real ydist
            local timer t = NewTimer()
            set D.caster = GetTriggerUnit()
            set D.target = GetSpellTargetUnit()
            set xdist = GetUnitX(D.caster) - GetUnitX(D.target)
            set ydist = GetUnitY(D.caster) - GetUnitY(D.target)
            set D.TotalDist = SquareRoot((xdist * xdist) + (ydist * ydist))
            set D.DistLeft = D.TotalDist
            call SetUnitPathing(D.caster, false)
            call PauseUnit(D.caster, true)
            call SetUnitAnimation(D.caster, "walk")
            set D.angle = bj_RADTODEG * (Atan2(GetUnitY(D.target) - GetUnitY(D.caster), GetUnitX(D.target) - GetUnitX(D.caster)))
            call SetTimerData(t, D)
            call TimerStart(t, 0.03, true, function thistype.Timer_Actions)
            set t = null
            return D
        endmethod
        
        static method Conditions takes nothing returns boolean
            if GetSpellAbilityId() == 'A001' then
                call thistype.create()
            endif
            return false
        endmethod
        
        static method onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
            call TriggerAddCondition(t, Condition(function thistype.Conditions))
            set t = null
        endmethod
    
    endstruct

endscope
 
Status
Not open for further replies.
Top