• 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.

Need to detect units facing my unit

Status
Not open for further replies.
Level 25
Joined
Jul 10, 2006
Messages
3,315
Here's my code for a front-only shield passive.

  • Actions
  • Set tempLoc1 = (Position of (DamageEventTarget))
  • Set tempLoc2 = (Position of (DamageEventSource))
  • Set Facing = (Facing of (DamageEventTarget))
  • Set Source = (Angle from tempLoc2 to tempLoc1)
  • Set Source = (tsSource + 180.00)
  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    • If - Conditions
      • Or - Any (Conditions) are true
        • Conditions
          • And - All (Conditions) are true
            • Conditions
              • (Facing - Source) Less than or equal to 45.00
              • (Facing - Source) Greater than or equal to -45.00
            • (Facing - Source) Greater than or equal to 315.00
        • Then - Actions
          • Set DMGS_Reduction = 0.5
        • Else - Actions
          • Custom script: call RemoveLocation(udg_tempLoc1)
          • Custom script: call RemoveLocation(udg_tempLoc2)
It's the same idea as facing a unit, you just need to change the unit variables and angles around.

I'd be a bit more helpful with my explanation if I didn't just wake up... let me know if you need more help.

EDIT: Fixed indentation.
 
Last edited:
Level 37
Joined
Mar 6, 2006
Messages
9,243
I'd do it like this:

  • BS3 Copy
    • Events
      • Unit - A unit Is attacked
    • Conditions
    • Actions
      • Set u1 = (Attacking unit)
      • Set u2 = (Attacked unit)
      • Set p1 = (Position of u1)
      • Set p2 = (Position of u2)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Cos(((Angle from p1 to p2) - (Facing of u2)))) Less than 0.00
          • (Acos((Cos(((Angle from p1 to p2) - (Facing of u2)))))) Greater than 90.00
        • Then - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: Frontstab!
        • Else - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: Backstab!
      • Custom script: call RemoveLocation(udg_p1)
      • Custom script: call RemoveLocation(udg_p2)


This tests if u1 is in front or behind u2.
The conditions are the same, use the one you prefer. The Acos line is in degrees, 90 translates to semicircle. The upper one has range from -1 to 1.
 
Level 25
Joined
Jul 10, 2006
Messages
3,315
I'd do it like this:

  • BS3 Copy
    • Events
      • Unit - A unit Is attacked
    • Conditions
    • Actions
      • Set u1 = (Attacking unit)
      • Set u2 = (Attacked unit)
      • Set p1 = (Position of u1)
      • Set p2 = (Position of u2)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Cos(((Angle from p1 to p2) - (Facing of u2)))) Less than 0.00
          • (Acos((Cos(((Angle from p1 to p2) - (Facing of u2)))))) Greater than 90.00
        • Then - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: Frontstab!
        • Else - Actions
          • Game - Display to Player Group - Player 1 (Red) the text: Backstab!
      • Custom script: call RemoveLocation(udg_p1)
      • Custom script: call RemoveLocation(udg_p2)


The conditions are the same, use the one you prefer. The Acos line is in degrees, 90 translates to semicircle. The upper one has range from -1 to 1.

Does this work for angles near 0?

When making my script, it bugged near angle 0 when using only the first two conditions.

EDIT: Indeed it does.
 
Level 30
Joined
Jul 23, 2009
Messages
1,033
Ruler, I used your trigger provided in your map as a base and now it works perfectly as I want :)

I got rid of the leaks I hope
  • Radiance Check
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Game - Display to (All players) the text: (String(AB_RadianceTimer[1]))
      • Unit Group - Pick every unit in AB_RadianceCasters and do (Actions)
        • Loop - Actions
          • Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • AB_RadianceTimer[(Player number of (Owner of (Picked unit)))] Greater than (>) 0.00
            • Then - Actions
              • Set AB_RadianceTimer[(Player number of (Owner of (Picked unit)))] = (AB_RadianceTimer[(Player number of (Owner of (Picked unit)))] - 1.00)
              • Set tempLoc1 = (Position of (Picked unit))
              • Set AB_LocalRadianceCaster = (Picked unit)
              • Set AB_TempRadTargetGroup = (Units within 200.00 of tempLoc1)
              • Unit Group - Pick every unit in AB_TempRadTargetGroup and do (Actions)
                • Loop - Actions
                  • Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Owner of (Picked unit)) Equal to (==) Neutral Hostile
                    • Then - Actions
                      • Set tempLoc2 = (Position of (Picked unit))
                      • Set tempFacing = (Facing of (Picked unit))
                      • Set tempAngle = (Angle from tempLoc2 to tempLoc1)
                        • Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
                          • If - Conditions
                            • Multiple ConditionsOr - Any (Conditions) are true
                              • Conditions
                                • Multiple ConditionsAnd - All (Conditions) are true
                                  • Conditions
                                    • (tempFacing - tempAngle) Less than or equal to (<=) 90.00
                                    • (tempFacing - tempAngle) Greater than or equal to (>=) -90.00
                                • (tempFacing - tempAngle) Greater than or equal to (>=) 270.00
                          • Then - Actions
                            • Unit - Cause AB_LocalRadianceCaster to damage (Picked unit), dealing ((Real((Intelligence of AB_LocalRadianceCaster (Include bonuses)))) x 2.00) damage of attack type Spells and damage type Normal
                          • Else - Actions
                    • Else - Actions
                  • Custom script: call RemoveLocation(udg_tempLoc2)
            • Else - Actions
              • Unit Group - Remove (Picked unit) from AB_RadianceCasters
      • Custom script: call RemoveLocation(udg_tempLoc1)
      • Custom script: call DestroyGroup(udg_AB_TempRadTargetGroup)
        • Multiple FunctionsIf (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • (AB_RadianceCasters is empty) Equal to (==) True
          • Then - Actions
            • Trigger - Turn off (This trigger)
          • Else - Actions
 
Status
Not open for further replies.
Top