• 🏆 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!

Unit play animation backwards when moving in the opposite direction of facing

Status
Not open for further replies.
Level 9
Joined
Sep 20, 2015
Messages
385
So the title is self explanatory of what my problem is. I use this two blocks of code. But the animation is still the same. I just started to use JASS and i need some help

Trigger of the demo map made by lolreported.

JASS:
globals
    unit array WASD_PlayerUnit


    boolean array WASD_IsKeyDown_W
    boolean array WASD_IsKeyDown_A
    boolean array WASD_IsKeyDown_S
    boolean array WASD_IsKeyDown_D

    constant oskeytype WASD_KEY_W = ConvertOsKeyType($57)
    constant oskeytype WASD_KEY_A = ConvertOsKeyType($41)
    constant oskeytype WASD_KEY_S = ConvertOsKeyType($53)
    constant oskeytype WASD_KEY_D = ConvertOsKeyType($44)

    constant real WASD_MoveDistance = 10.0
   

endglobals

function PlayerMoveEvent_Actions takes nothing returns nothing
    local integer i = 0
    local location loc1 = null
    local location loc2 = null 
    loop
    exitwhen(i > 24)
        if WASD_IsKeyDown_W[i] == true and WASD_IsKeyDown_D[i] == false and WASD_IsKeyDown_A[i] == false and WASD_IsKeyDown_S[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 90.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 90)
            set udg_WASD_Unit_Facing_Direction[i] = 90
             
           

            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

        elseif WASD_IsKeyDown_S[i] == true and WASD_IsKeyDown_D[i] == false and WASD_IsKeyDown_A[i] == false and WASD_IsKeyDown_W[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 270.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 270)
            set udg_WASD_Unit_Facing_Direction[i] = 270
           
           
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

        elseif WASD_IsKeyDown_A[i] == true and WASD_IsKeyDown_D[i] == false and WASD_IsKeyDown_W[i] == false and WASD_IsKeyDown_S[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 180.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 180)
            set udg_WASD_Unit_Facing_Direction[i] = 180
           
             
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

        elseif WASD_IsKeyDown_D[i] and WASD_IsKeyDown_W[i] == false and WASD_IsKeyDown_A[i] == false and WASD_IsKeyDown_S[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 0.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 0)
            set udg_WASD_Unit_Facing_Direction[i] = 0
           
             
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)
        endif
        //OTHER DIRECTIONS TESTS IF AND -----DIAGONALS---
       
       if (WASD_IsKeyDown_W[i]) and WASD_IsKeyDown_D[i] and WASD_IsKeyDown_A[i] == false and WASD_IsKeyDown_S[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 45.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 45)
            set udg_WASD_Unit_Facing_Direction[i] = 45
           
             
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)
       
       elseif (WASD_IsKeyDown_W[i]) and WASD_IsKeyDown_A[i] and WASD_IsKeyDown_D[i] == false and WASD_IsKeyDown_S[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 135.0)
            //call SetUnitFacing(udg_PlayerUnit[i], 135)
            set udg_WASD_Unit_Facing_Direction[i] = 135
       
               
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

       elseif (WASD_IsKeyDown_S[i]) and WASD_IsKeyDown_D[i] and WASD_IsKeyDown_A[i] == false and WASD_IsKeyDown_W[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 315.0)
            set udg_WASD_Unit_Facing_Direction[i] = 315
           
           
           
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

       elseif WASD_IsKeyDown_S[i] == true and WASD_IsKeyDown_A[i] == true and WASD_IsKeyDown_D[i] == false and WASD_IsKeyDown_W[i] == false then
            set loc1 = Location(GetUnitX(udg_PlayerUnit[i]), GetUnitY(udg_PlayerUnit[i]))
            set loc2 = PolarProjectionBJ(loc1, udg_WASD_PlayerMoveDistance[i], 225.0)
            set udg_WASD_Unit_Facing_Direction[i] = 225
           
           
           
            call SetUnitX( udg_PlayerUnit[i],GetLocationX(loc2))
            call SetUnitY( udg_PlayerUnit[i],GetLocationY(loc2))
            call ConditionalTriggerExecute( gg_trg_WASD_FacingAnim )
            call RemoveLocation(loc1)
            call RemoveLocation(loc2)

        endif
    set i = i + 1
    endloop
    


    set loc1 = null
    set loc2 = null
endfunction

function PlayerKeyEvent_Actions takes nothing returns nothing
    local oskeytype osKeyPressed = BlzGetTriggerPlayerKey() // What key was pressed or released?
    local boolean isKeyDown = BlzGetTriggerPlayerIsKeyDown() // Was the event key pressed or released?
    local integer i=0
   
    //Not used here, but this way we can detect the meta-key.
    //local integer metaKeyPressed = BlzGetTriggerPlayerMetaKey()

    loop
    exitwhen ( i > 24 )
    if (osKeyPressed == WASD_KEY_W) then
        set WASD_IsKeyDown_W[GetPlayerId(GetTriggerPlayer())] = isKeyDown
        if isKeyDown == true then
             set udg_WASD_IsKeyDown_W[GetPlayerId(GetTriggerPlayer())] = true
            call SetUnitAnimationByIndex (udg_PlayerUnit[i], udg_AnimationIndexArray[i])

        else
             set udg_WASD_IsKeyDown_W[GetPlayerId(GetTriggerPlayer())] = false
                         
        endif

        call ConditionalTriggerExecute( gg_trg_WASDKeyPressed )
         
    elseif (osKeyPressed == WASD_KEY_A) then
        set WASD_IsKeyDown_A[GetPlayerId(GetTriggerPlayer())] = isKeyDown
        if isKeyDown == true then
             set udg_WASD_IsKeyDown_A[GetPlayerId(GetTriggerPlayer())] = true
            call SetUnitAnimationByIndex (udg_PlayerUnit[i], udg_AnimationIndexArray[i])

        else
             set udg_WASD_IsKeyDown_A[GetPlayerId(GetTriggerPlayer())] = false
                         
        endif
       
        call ConditionalTriggerExecute( gg_trg_WASDKeyPressed )

    elseif (osKeyPressed == WASD_KEY_S) then
        set WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = isKeyDown
        if isKeyDown == true then
             set udg_WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = true
            call SetUnitAnimationByIndex (udg_PlayerUnit[i], udg_AnimationIndexArray[i])

        else
             set udg_WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = false
                         
        endif
        call ConditionalTriggerExecute( gg_trg_WASDKeyPressed )

    elseif (osKeyPressed == WASD_KEY_D) then
        set WASD_IsKeyDown_D[GetPlayerId(GetTriggerPlayer())] = isKeyDown
        if isKeyDown == true then
             set udg_WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = true
            call SetUnitAnimationByIndex (udg_PlayerUnit[i], udg_AnimationIndexArray[i])
             
        else
             set udg_WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = false
               
        endif
        call ConditionalTriggerExecute( gg_trg_WASDKeyPressed )

    endif
    set i = i + 1
    endloop
endfunction

//===========================================================================
function InitTrig_WASD_PlayerKeyEvent_8_dire takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    local integer i = 0

    //takes trigger whichTrigger, player whichPlayer, oskeytype key, integer metaKey, boolean keyDown returns event

    /* metaKey = In order to fire the event the player must press down another key at the same time,
       0 = No additional key required.
       1 = Shift-key required
       2 = Ctrl-key required
       3 = ????
       4 = Alt-key required
       5+ = ????

    */

    loop
    exitwhen(i > 24)
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, false )// Releases the "D"-key.
    set i = i + 1
    endloop

    call TriggerAddAction( t, function PlayerKeyEvent_Actions )

    set t = null

    set t = CreateTrigger(  )
    call TriggerRegisterTimerEvent( t, 0.03, true )
    call TriggerAddAction( t, function PlayerMoveEvent_Actions )

endfunction

  • WASD FacingAnim
    • Events
    • Conditions
    • Actions
      • Custom script: local integer i = GetPlayerId(GetTriggerPlayer())
      • Custom script: if ( ( AcosBJ(udg_WASD_Unit_Facing_Direction[i]) - AngleBetweenPoints(GetUnitLoc(udg_PlayerUnit[i]), udg_MousePosition[i]) ) < 45.00 ) then
      • Custom script: call SetUnitTimeScale(udg_PlayerUnit[i], -1)
      • Custom script: else
      • Custom script: call SetUnitTimeScale(udg_PlayerUnit[i], 1)
      • Custom script: endif
      • Custom script: if ( not ( ( AcosBJ(udg_WASD_Unit_Facing_Direction[i]) - AngleBetweenPoints(GetUnitLoc(udg_PlayerUnit[i]), udg_MousePosition[i]) ) < 45.00 ) ) then
      • Custom script: if ( not ( ( AcosBJ(udg_WASD_Unit_Facing_Direction[i]) - AngleBetweenPoints(GetUnitLoc(WASD_PlayerUnit[i]), udg_MousePosition[i]) ) < 45.00 ) ) then
      • Custom script: call SetUnitTimeScale(udg_PlayerUnit[i], -1)
  • UnitFacing
    • Events
      • Player - Player 1 (Red) issues Mouse Move event
    • Conditions
    • Actions
      • Set MousePosition[(Player number of (Triggering player))] = (Mouse Position for Triggered Mouse Event)
      • Unit - Make PlayerUnit[1] face (Angle from (Position of PlayerUnit[1]) to MousePosition[1]) over 0.00 seconds
      • Custom script: call SetUnitFacing(udg_PlayerUnit[GetPlayerId(GetTriggerPlayer())], AngleBetweenPoints(GetUnitLoc(udg_PlayerUnit[GetPlayerId(GetTriggerPlayer())]),udg_MousePosition[GetPlayerId(GetTriggerPlayer())]))
      • Custom script: call RemoveLocation (udg_MousePosition[GetPlayerId(GetTriggerPlayer())])
Thanks for the help
 
Level 39
Joined
Feb 27, 2007
Messages
5,022
  1. It doesn't work because your angle comparison is just... way wrong. You're using Acos on an angle, then subtracting another angle from that result. What are you trying to check? I absolutely cannot decipher what you meant to do here. Something about 45 degrees it seems.
  2. You're leaking a large number of locations. Things like GetUnitLoc(). Things That Leak
  3. Don't use locations, use xy coordinates directly. JASS has easy access to these coordinates while GUI is (usually) forced to use locations.
  4. You can move the entirety of the WASD FacingAnim trigger into a new function in your JASS script; there's no reason to have it as a trigger where every line is a custom script. There are also some unclosed if
  5. Why are there some globals declared with udg_ counterparts? You're not even using these variables in the GUI code I see.
  6. You're setting unit facing twice in the UnitFacing trigger, but the first time uses index [1] while the second uses index [player id of triggering player]. I presume the second one is what you meant to do?
  7. In the PlayerKeyEvent_Actions function you do not need to loop i over 0-24; the way you are doing it right now will not work for multiplayer because you're setting all players' unit facing/animation when any player presses or releases a WASD key. Instead just use i = GetPlayerId(GetTriggerPlayer()).
  8. Your code can be simplified and compressed by only putting the differing actions in the elseif tree, instead of also all the actions that are identical every time.
  9. Where is udg_AnimationIndexArray[] being set and why is it using a looped player number as its index?
  10. What does the WASDKeyPressed trigger do? Why is that separate from this script?
  11. Since you are not covering every possible combination of keys pressed, if a player pressed WSD simultaneously the unit would not move at all. Any 3 or 4 key combination will work this way. I think the easiest thing to do would be to sum up all of the pressed inputs (W cancels S, A cancels D), compute the angle that results in, and then use that angle.
  12. Booleans don't need to be compared to == true or == false, you can use use the boolean (or not + the boolean for false) directly.
  13. You can use a timer instead of a trigger to run the function periodically.
JASS:
globals
    unit array WASD_PlayerUnit

    boolean array WASD_IsKeyDown_W
    boolean array WASD_IsKeyDown_A
    boolean array WASD_IsKeyDown_S
    boolean array WASD_IsKeyDown_D

    constant oskeytype WASD_KEY_W = ConvertOsKeyType($57)
    constant oskeytype WASD_KEY_A = ConvertOsKeyType($41)
    constant oskeytype WASD_KEY_S = ConvertOsKeyType($53)
    constant oskeytype WASD_KEY_D = ConvertOsKeyType($44)

    constant real WASD_MoveDistance = 10.0
endglobals


function PlayerMoveEvent_Actions takes nothing returns nothing
    local integer i = 0
    local real dx
    local real dy
    local real angle

    loop
    exitwhen(i > 24)
        set dx = 0.
        set dy = 0.

        if WASD_IsKeyDown_W[i] then
            set dy = dy + 1.
        elseif WASD_IsKeyDown_S[i] then
            set dy = dy - 1.
        elseif WASD_IsKeyDown_A[i] then
            set dy = dx + 1.
        elseif WASD_IsKeyDown_D[i] then
            set dy = dx - 1.
        endif

        set angle = Atan2(dy, dx) //gives the angle, in radians not degrees
        call SetUnitX(udg_PlayerUnit[i], GetUnitX(udg_PlayerUnit[i]) + udg_WASD_PlayerMoveDistance[i]*Cos(angle)) //Cos takes radians input, CosBJ takes degrees input
        call SetUnitY(udg_PlayerUnit[i], GetUnitY(udg_PlayerUnit[i]) + udg_WASD_PlayerMoveDistance[i]*Sin(angle)) //Sin takes radians input, SinBJ takes degrees input

        //DO YOUR ANGLE CHECK FOR ANIMATION STUFF HERE

        set i = i+1
    endloop
endfunction

function PlayerKeyEvent_Actions takes nothing returns nothing
    local oskeytype osKeyPressed = BlzGetTriggerPlayerKey() // What key was pressed or released?
    local boolean isKeyDown = BlzGetTriggerPlayerIsKeyDown() // Was the event key pressed or released?
    local integer i=GetPlayerId(GetTriggerPlayer())
  
    //Not used here, but this way we can detect the meta-key.
    //local integer metaKeyPressed = BlzGetTriggerPlayerMetaKey()


    if (osKeyPressed == WASD_KEY_W) then
        set WASD_IsKeyDown_W[i] = isKeyDown

    elseif (osKeyPressed == WASD_KEY_A) then
        set WASD_IsKeyDown_A[i] = isKeyDown

    elseif (osKeyPressed == WASD_KEY_S) then
        set WASD_IsKeyDown_S[i] = isKeyDown

    elseif (osKeyPressed == WASD_KEY_D) then
        set WASD_IsKeyDown_D[i] = isKeyDown
    endif

    if isKeyDown then
        call SetUnitAnimationByIndex (udg_PlayerUnit[i], udg_AnimationIndexArray[i])
        // Not sure what this does?
        // call ConditionalTriggerExecute( gg_trg_WASDKeyPressed )
    endif
endfunction

//===========================================================================
function InitTrig_WASD_PlayerKeyEvent_8_dire takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    local integer i = 0

    //takes trigger whichTrigger, player whichPlayer, oskeytype key, integer metaKey, boolean keyDown returns event

    /* metaKey = In order to fire the event the player must press down another key at the same time,
       0 = No additional key required.
       1 = Shift-key required
       2 = Ctrl-key required
       3 = ????
       4 = Alt-key required
       5+ = ????

    */

    loop
    exitwhen(i > 24)
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, false )// Releases the "D"-key.
    set i = i + 1
    endloop

    call TriggerAddAction( t, function PlayerKeyEvent_Actions )
    set t = null

    call TimerStart(CreateTimer(), 0.03, true, function PlayerMoveEvent_Actions)
endfunction
 
Level 9
Joined
Sep 20, 2015
Messages
385
It doesn't work because your angle comparison is just... way wrong. You're using Acos on an angle, then subtracting another angle from that result. What are you trying to check? I absolutely cannot decipher what you meant to do here. Something about 45 degrees it seems

1 What i want to do is that when the unit move in a direction that is opposite from the facing direction of the mouse the animations is played backwards. I tried to do this with a backstab equation i found.


You're leaking a large number of locations. Things like GetUnitLoc(). Things That Leak

2 Thanks i will remove the leaks, this was just a test so it's not polished or optimized.

  • Don't use locations, use xy coordinates directly. JASS has easy access to these coordinates while GUI is (usually) forced to use locations.
  • You can move the entirety of the WASD FacingAnim trigger into a new function in your JASS script; there's no reason to have it as a trigger where every line is a custom script. There are also some unclosed if
  • Why are there some globals declared with udg_ counterparts? You're not even using these variables in the GUI code I see.
  • You're setting unit facing twice in the UnitFacing trigger, but the first time uses index [1] while the second uses index [player id of triggering player]. I presume the second one is what you meant to do?
  • In the PlayerKeyEvent_Actions function you do not need to loop i over 0-24; the way you are doing it right now will not work for multiplayer because you're setting all players' unit facing/animation when any player presses or releases a WASD key. Instead just use i = GetPlayerId(GetTriggerPlayer()).

3 Thanks i will change these.
Some of the functions in the GUI triggers are disabled, i just leave them there in case i need them.

  • Where is udg_AnimationIndexArray[] being set and why is it using a looped player number as its index?
  • What does the WASDKeyPressed trigger do? Why is that separate from this script?

4 The variable is set on a map initialization trigger. I used this because with the string function it didn't worked well, and i need this becasue the walk animation index is different depending on the unit model.

5 WASDKeyPressed trigger is to reset the animation when a key is released. It's separated because is in GUI.


Thanks you i will try to make the changes you suggested.

Edit: So i made some changes and used your code but now the unit does not move at all.
 
Last edited:
Level 9
Joined
Sep 20, 2015
Messages
385
The full WASD movement Trigger Code :
JASS:
globals



    boolean array WASD_IsKeyDown_W
    boolean array WASD_IsKeyDown_A
    boolean array WASD_IsKeyDown_S
    boolean array WASD_IsKeyDown_D



    constant oskeytype WASD_KEY_W = ConvertOsKeyType($57)
    constant oskeytype WASD_KEY_A = ConvertOsKeyType($41)
    constant oskeytype WASD_KEY_S = ConvertOsKeyType($53)
    constant oskeytype WASD_KEY_D = ConvertOsKeyType($44)



    constant real WASD_MoveDistance = 10.0

    real array Fangl

endglobals

function PlayerMoveEvent_Actions takes nothing returns nothing
    //local integer i = GetPlayerId(GetTriggerPlayer())
    local integer i = GetPlayerId(GetOwningPlayer(  udg_PXHero[GetPlayerId(GetTriggerPlayer())] ))
    local real array dx
    local real array dy
    local real array angle
    local real array angleDEG
    local location array TerrainCheck
    local location array BlockPoint
    //local real array angle

 
 
        set dx[i] = 0.
        set dy[i] = 0.



        if WASD_IsKeyDown_W[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i]))  then
            set dy[i] = dy[i] + 1.
 
        elseif WASD_IsKeyDown_S[i] and (not ( WASD_IsKeyDown_W[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] - 1.
 
        elseif WASD_IsKeyDown_A[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_W[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dx[i] = dx[i] - 1.
 
        elseif WASD_IsKeyDown_D[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_W[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dx[i] = dx[i] + 1.

        elseif WASD_IsKeyDown_W[i] and WASD_IsKeyDown_D[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] + 0.5
            set dx[i] = dx[i] + 0.5
        elseif WASD_IsKeyDown_W[i] and WASD_IsKeyDown_A[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] + 0.5
            set dx[i] = dx[i] - 0.5
        elseif WASD_IsKeyDown_S[i] and WASD_IsKeyDown_D[i] and (not ( WASD_IsKeyDown_W[i] or WASD_IsKeyDown_A[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] - 0.5
            set dx[i] = dx[i] + 0.5
        elseif WASD_IsKeyDown_S[i] and WASD_IsKeyDown_A[i] and (not ( WASD_IsKeyDown_W[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] - 0.5
            set dx[i] = dx[i] - 0.5

        endif
   
 
   set angle[i] = Atan2(dy[i], dx[i]) //gives the angle, in radians not degrees

    if WASD_IsKeyDown_W[i] or WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_D[i] and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) then
     
        set udg_Temp_Point_A[i] = Location( (GetUnitX(udg_PXHero[i]) + 100*Cos(angle[i])) ,(GetUnitY(udg_PXHero[i]) + 100*Sin(angle[i])) )
         


       if IsPointWalkable(GetLocationX(udg_Temp_Point_A[i]), GetLocationY(udg_Temp_Point_A[i])) then
       
   
        call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Cos(angle[i]))
        call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Sin(angle[i]))
 
           

        elseif IsTerrainPathable( GetLocationX(udg_Temp_Point_A[i]), GetLocationY(udg_Temp_Point_A[i]), PATHING_TYPE_FLOATABILITY) then
                   
        elseif GetUnitAbilityLevel(udg_PXHero[i], 'A00M') > 0 then

          call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Cos(angle[i]))
          call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Sin(angle[i]))
          call BJDebugMsg(I2S(i))
                   
           else

          call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]))
          call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]))
 
        endif

    endif

     
    call RemoveLocation ( udg_Temp_Point_A[i]
 
endfunction

function PlayerKeyEvent_Actions takes nothing returns nothing
    local oskeytype osKeyPressed = BlzGetTriggerPlayerKey() // What key was pressed or released?
    local boolean isKeyDown = BlzGetTriggerPlayerIsKeyDown() // Was the event key pressed or released?
    local integer i=GetPlayerId(GetTriggerPlayer())

    local effect testef

    //Not used here, but this way we can detect the meta-key.
    local integer metaKeyPressed = BlzGetTriggerPlayerMetaKey()
 
     call DisplayTimedTextToForce( GetPlayersAll(), 1.00, I2S(BlzGetTriggerPlayerMetaKey())  )

     //loop
    //exitwhen (i > 24)
    if (osKeyPressed == WASD_KEY_W) then
        set WASD_IsKeyDown_W[i] = isKeyDown
 
    elseif (osKeyPressed == WASD_KEY_A) then
        set WASD_IsKeyDown_A[i] = isKeyDown

    elseif (osKeyPressed == WASD_KEY_S) then
        set WASD_IsKeyDown_S[i] = isKeyDown

    elseif (osKeyPressed == WASD_KEY_D) then
        set WASD_IsKeyDown_D[i] = isKeyDown
    endif


    if isKeyDown and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
     
           call SetUnitAnimationByIndex (udg_PXHero[i], udg_AnimationIndexArray[i])

           call BlzUnitDisableAbility ( udg_PXHero[i], 'Aatk' , true, false)

           call BJDebugMsg(I2S(i))

       else
         call BlzUnitDisableAbility ( udg_PXHero[i], 'Aatk' , false, false)
     
        if WASD_IsKeyDown_W[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_S[i] or WASD_IsKeyDown_D[i] and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) then
 
            else
       
            call ResetUnitAnimation( udg_PXHero[i] )
       
            endif

    endif
endfunction

//===========================================================================
function InitTrig_WASD_PlayerKeyEvent_8_direMetakeytest takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    local integer i = 0
 
    //takes trigger whichTrigger, player whichPlayer, oskeytype key, integer metaKey, boolean keyDown returns event

    /* metaKey = In order to fire the event the player must press down another key at the same time,
       0 = No additional key required.
       1 = Shift-key required
       2 = Ctrl-key required
       3 = ????
       4 = Alt-key required
       5+ = ????

    */

    loop

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 0, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 0, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 0, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 0, false )// Releases the "D"-key.


    //METAKEY 1 SHIFT


        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 1, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 1, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 1, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 1, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 1, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 1, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 1, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 1, false )// Releases the "D"-key.

    //METAKEY 2 CTRL

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 2, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 2, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 2, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 2, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 2, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 2, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 2, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 2, false )// Releases the "D"-key.

    //MEETAKEY 3 ??

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 3, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 3, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 3, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 3, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 3, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 3, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 3, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 3, false )// Releases the "D"-key.

    //METAKEY 4 ALT

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 4, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 4, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 4, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 4, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 4, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 4, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 4, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 4, false )// Releases the "D"-key.

    //METAKEY 5 ???


        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 5, true ) // Presses the "W"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_W, 5, false )// Releases the "W"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 5, true ) // Presses the "A"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_A, 5, false )// Releases the "A"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 5, true ) // Presses the "S"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_S, 5, false )// Releases the "S"-key.

        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 5, true ) // Presses the "D"-key.
        call BlzTriggerRegisterPlayerKeyEvent( t, Player(i), WASD_KEY_D, 5, false )// Releases the "D"-key.

    set i = i + 1
    exitwhen(i > 24)
    endloop

    call TriggerAddAction( t, function PlayerKeyEvent_Actions )

    //set t = null

    call TimerStart(CreateTimer(), 0.03, true, function PlayerMoveEvent_Actions)

    //set t = CreateTrigger(  )
    //call TriggerRegisterTimerEvent( t, 0.03, true )
    //call TriggerAddAction( t, function PlayerMoveEvent_Actions )

endfunction




I thought this trigger worked for all players but the timer actions ( function PlayerMoveEvent_Actions, when the unit PXHero is moved) seems to work only for player red.

I edited the code a little, hopefully you can read it better now.

I'll explain what i added

I added the diagonal directions for the dx dy variables

JASS:
        elseif WASD_IsKeyDown_W[i] and WASD_IsKeyDown_D[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] + 0.5
            set dx[i] = dx[i] + 0.5
        elseif WASD_IsKeyDown_W[i] and WASD_IsKeyDown_A[i] and (not ( WASD_IsKeyDown_S[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] + 0.5
            set dx[i] = dx[i] - 0.5
        elseif WASD_IsKeyDown_S[i] and WASD_IsKeyDown_D[i] and (not ( WASD_IsKeyDown_W[i] or WASD_IsKeyDown_A[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] - 0.5
            set dx[i] = dx[i] + 0.5
        elseif WASD_IsKeyDown_S[i] and WASD_IsKeyDown_A[i] and (not ( WASD_IsKeyDown_W[i] or WASD_IsKeyDown_D[i])) and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
            set dy[i] = dy[i] - 0.5
            set dx[i] = dx[i] - 0.5

        endif





I added a walkability check with udg_Temp_Point_A after the angle variable is set and then if the unit has an ability (A00M) it can walk on deep water otherwise it will not move and is stopped by deep water, terrain blockers, destructibles, buildings ecc.


JASS:
if WASD_IsKeyDown_W[i] or WASD_IsKeyDown_S[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_D[i] and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) then
     
        set udg_Temp_Point_A[i] = Location( (GetUnitX(udg_PXHero[i]) + 100*Cos(angle[i])) ,(GetUnitY(udg_PXHero[i]) + 100*Sin(angle[i])) )
 
       if IsPointWalkable(GetLocationX(udg_Temp_Point_A[i]), GetLocationY(udg_Temp_Point_A[i])) then
       
   
        call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Cos(angle[i]))
        call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Sin(angle[i]))

           

        elseif IsTerrainPathable( GetLocationX(udg_Temp_Point_A[i]), GetLocationY(udg_Temp_Point_A[i]), PATHING_TYPE_FLOATABILITY) then
                   
        elseif GetUnitAbilityLevel(udg_PXHero[i], 'A00M') > 0 then

          call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Cos(angle[i]))
          call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]) + udg_WASD_PlayerMoveDistance[i]*Sin(angle[i]))
          call BJDebugMsg(I2S(i))
                   
           else

          call SetUnitX(udg_PXHero[i], GetUnitX(udg_PXHero[i]))
          call SetUnitY(udg_PXHero[i], GetUnitY(udg_PXHero[i]))
 
        endif



I added metakey detection in another trigger , these are globals variables i added to make the trigger work even if the shift key is pressed or not

JASS:
 (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i]))


In the other function (PlayerKeyEvent_Actions) I added the animation play action and disable the unit attack while moving, also added the Shift variables in the conditions.

JASS:
 if isKeyDown and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) and ( isLeftMbDown[i] or not(isLeftMbDown[i])) then
     
           call SetUnitAnimationByIndex (udg_PXHero[i], udg_AnimationIndexArray[i])

           call BlzUnitDisableAbility ( udg_PXHero[i], 'Aatk' , true, false)

           call BJDebugMsg(I2S(i))

       else
         call BlzUnitDisableAbility ( udg_PXHero[i], 'Aatk' , false, false)
     
        if WASD_IsKeyDown_W[i] or WASD_IsKeyDown_A[i] or WASD_IsKeyDown_S[i] or WASD_IsKeyDown_D[i] and (IsKeyDown_SHIFT[i] or not ( IsKeyDown_SHIFT[i])) then
 
            else
       
            call ResetUnitAnimation( udg_PXHero[i] )
       
            endif
 
Last edited:
Status
Not open for further replies.
Top