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

How to make unit face another unit

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

In many RP maps, there is the command 'fs which causes the selected units to turn and face where the spawner is.

How is this done? I'd like to make it so when a unit talks to another (via smart command), the unit being talked turns and faces the talker.
 
Level 13
Joined
Mar 24, 2013
Messages
1,105
JASS:
native SetUnitFacing takes unit whichUnit, real facingAngle returns nothing

JASS:
native SetUnitFacingTimed takes unit whichUnit, real facingAngle, real duration returns nothing

JASS:
function SetUnitFacingToFaceLocTimed takes unit whichUnit, location target, real duration returns nothing
    local location unitLoc = GetUnitLoc(whichUnit)

    call SetUnitFacingTimed(whichUnit, AngleBetweenPoints(unitLoc, target), duration)
    call RemoveLocation(unitLoc)
endfunction

These?
 
This is what I use in my map:
JASS:
method setFacingTowardsUnit takes unit B returns nothing 
            call SetUnitFacing(this.unit, Atan2(GetUnitY(B) - GetUnitY(this.unit), GetUnitX(B) - GetUnitX(this.unit)) * bj_RADTODEG)
        endmethod
this.unit refers to the source unit. I suppose it would look something like this:
JASS:
function SetUnitFacingTowardsUnit takes unit A, unit toFaceTowards returns nothing 
    call SetUnitFacing(A, Atan2(GetUnitY(toFaceTowards) - GetUnitY(A), GetUnitX(toFaceTowards) - GetUnitX(A)) * bj_RADTODEG)
endfunction

Atan2(endY - startY, endX - startX) calculates the angle between two points, and then multiplying it by bj_RADTODEG (180/pi) will convert the radians into degrees (so that it can be used in SetUnitFacing()).
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Oh wow I didn't think you'd actually have to calculate the degrees! I figured there was just a native that caused a unit to face the intrinsic front of another unit.
 
Status
Not open for further replies.
Top