• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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
Hmmm, I changed the xs and ys around, but it still does the same thing.

EDIT: You can play animations while a unit is paused. I just tested with the unit's "attack" animation. However, the "walk" animation only plays for a fraction of a second.
 
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