Unit wont move

Dryvnt

New Member
Reaction score
10
I tried to optimize my code, but now it wont work... I don't see why the function
JASS:
call SetUnitX(whichUnit, newX)
wont work... I have set all variable right, and everything else but THAT function (and SetUnitY) wont work! :(

Here's code:
JASS:
library PhysLib

    globals
        private integer wut = 0
    endglobals

    function Rtrue takes nothing returns boolean
        return true
    endfunction
    
    private function PCountsUnitsInGroupEnum takes nothing returns nothing
        set wut = wut + 1
    endfunction

    
    private function PCountUnitsInGroup takes group g returns integer
        set wut = 0
        call ForGroup(g, function PCountsUnitsInGroupEnum)
        call DestroyGroup(g)
        return wut
    endfunction
    
    function PhysApplyForceX takes real XForce, unit whichUnit returns nothing
        local integer CustomID = GetUnitUserData(whichUnit)
        set X[CustomID] = X[CustomID] + XForce //Anything above 2000 will probably make your unit go completely out of the map
    endfunction
    
    function PhysApplyForceY takes real YForce, unit whichUnit returns nothing
        local integer CustomID = GetUnitUserData(whichUnit)
        set Y[CustomID] = Y[CustomID] + YForce //Anything above 2000 will probably make your unit go completely out of the map
    endfunction
    
    function PhysApplyForceZ takes real ZForce, unit whichUnit returns nothing
        local integer CustomID = GetUnitUserData(whichUnit)
        set Z[CustomID] = Z[CustomID] + (ZForce * 0.1)
    endfunction
    
    function PhysApplyForceAngle takes unit whichUnit, real Angle, real Force returns nothing
        call PhysApplyForceX(Cos(Angle * bj_DEGTORAD) * Force, whichUnit)
        call PhysApplyForceY(Sin(Angle * bj_DEGTORAD) * Force, whichUnit)
    endfunction
    
    function PhysUpdateUnits takes nothing returns nothing
        local unit FOG
        local integer Index = 0
        local rect TurnRect = bj_mapInitialPlayableArea
        set PhysUnits = GetUnitsInRectMatching(GetWorldBounds(), Filter(function Rtrue))
        set PhysNumUnits = PCountUnitsInGroup(PhysUnits)
        loop
        exitwhen Index == PhysNumUnits
            set FOG = FirstOfGroup(PhysUnits)
            call SetUnitUserData( FOG, Index )
            set CurrentUnit[Index] = FOG
            if GetUnitX(FOG) > GetRectMaxX(TurnRect) or GetUnitX(FOG) < GetRectMinX(TurnRect) then
                set X[Index] = -X[Index]
            endif
            if GetUnitY(FOG) > GetRectMaxY(TurnRect) or GetUnitY(FOG) < GetRectMinY(TurnRect) then
                set Y[Index] = -Y[Index]
            endif
            set Index = Index + 1
            call GroupRemoveUnit(PhysUnits, FOG)
        endloop
        set FOG = null
    endfunction
    
    function PhysCreateUnit takes player id, integer unitid, real x, real y, real face returns unit
        local unit a = CreateUnit(id,unitid,x,y,face)
        call PhysUpdateUnits()
        return a
    endfunction
endlibrary

scope Physics initializer Start

globals
group PhysUnits = CreateGroup()
integer PhysNumUnits = 0
real array X
real array Y
real array Z
unit array CurrentUnit
endglobals

function PhysGetUnitZ takes unit whichUnit returns real
        local location Loc = GetUnitLoc(whichUnit)
        local real ret = GetLocationZ (Loc) + GetUnitFlyHeight (whichUnit)
        call RemoveLocation(Loc)
        set Loc = null
        return ret
    endfunction

function PhysicsMain takes nothing returns nothing
    local integer Index = 0
    local real Sign
    local location TempLoc
    local location TempLoc1
    local location TempLocX
    local location TempLocY
    call PhysUpdateUnits()
    loop
    exitwhen Index >= PhysNumUnits
        if X[Index] != 0 or Y[Index] != 0 then
            call BJDebugMsg(R2S(X[GetUnitUserData(CurrentUnit[Index])])) //This is a debugger, the text shows.
            call BJDebugMsg(R2S(GetUnitX(CurrentUnit[Index]))) //Same as above
            call SetUnitX(CurrentUnit[Index], (GetUnitX(CurrentUnit[Index]) + X[Index]))
            call SetUnitY(CurrentUnit[Index], (GetUnitY(CurrentUnit[Index]) + Y[Index]))

            if X[Index] > -1 and X[Index] < 1 then
                set X[Index] = 0
            elseif GetUnitFlyHeight(CurrentUnit[Index]) < 5 then
                set Sign = RSignBJ(X[Index])
                set X[Index] = X[Index] - ((X[Index] / (Sign * 10)) * Sign)
            endif
            if Y[Index] > -1 and Y[Index] < 1 then
                set Y[Index] = 0
            elseif GetUnitFlyHeight(CurrentUnit[Index]) < 5 then
                set Sign = RSignBJ(Y[Index])
                set Y[Index] = Y[Index] - ((Y[Index] / (Sign * 10)) * Sign)
            endif
        endif
        set TempLoc = GetUnitLoc(CurrentUnit[Index])
        set TempLoc1 = Location((GetLocationX(TempLoc) + X[Index]), (GetLocationY(TempLoc) + Y[Index]))
        set TempLocX = Location((GetLocationX(TempLoc) + (X[Index])), GetLocationY(TempLoc))
        set TempLocY = Location(GetLocationX(TempLoc), (GetLocationY(TempLoc) + (Y[Index])))
        if GetUnitFlyHeight(CurrentUnit[Index]) + (60 * (X[Index] * 0.01) * RSignBJ(X[Index])) < ((GetLocationZ(TempLocX) - GetLocationZ(TempLoc))) then
            set X[Index] = -X[Index] * 0.1
        endif
        if GetUnitFlyHeight(CurrentUnit[Index]) + (60 * (X[Index] * 0.01) * RSignBJ(Y[Index])) < ((GetLocationZ(TempLocY) - GetLocationZ(TempLoc))) then
            set Y[Index] = -Y[Index] * 0.1
        endif        
        call SetUnitFlyHeight(CurrentUnit[Index], (GetUnitFlyHeight(CurrentUnit[Index]) + (GetLocationZ(TempLoc) - GetLocationZ(TempLoc1))), 100000)
        if Z[Index] <= 2 and PhysGetUnitZ(CurrentUnit[Index]) < (5 + GetLocationZ(TempLoc)) then
            if Z[Index] >= -2 then
                set Z[Index] = 0
            else
                set Z[Index] = -(Z[Index] * 0.5)
            endif
        else
            call UnitAddAbility(CurrentUnit[Index], 'Arav')
            call SetUnitFlyHeight(CurrentUnit[Index], (GetUnitFlyHeight(CurrentUnit[Index]) + Z[Index]), 100000)
            call UnitRemoveAbility(CurrentUnit[Index], 'Arav')
            set Z[Index] = Z[Index] - 1
        endif
    call RemoveLocation(TempLoc)
    call RemoveLocation(TempLoc1)
    call RemoveLocation(TempLocX)
    call RemoveLocation(TempLocY)
    set TempLoc = null
    set TempLoc1 = null
    set TempLocX = null
    set TempLocY = null
    set Index = (Index + 1)
    endloop
endfunction

function Start takes nothing returns nothing
    local timer CallPhysics = CreateTimer()
    local unit test = CreateUnit(Player(0), 'hpea', 0, 0, 0)
    call PhysUpdateUnits()
    call PhysApplyForceX(1000,test)
    call SetCameraTargetController(test,0,0,false)
    call TimerStart(CallPhysics, 0.05, true, function PhysicsMain)
endfunction

endscope


Edit:

I made the custom function "PCountUnitsInGroup" so that i wouldn't have to set bj_wantDestroyGroup to true :D
 

Trollvottel

never aging title
Reaction score
262
before reading through the whole code you should just try to look for the simplest answers. and there was no statement about the unit movespeed...
 

saw792

Is known to say things. That is all.
Reaction score
280
The point you seemed to miss, Dryvnt, was that SetUnitPosition(), SetUnitX() and SetUnitY() sometimes don't work when the unit has no movespeed.
 

saw792

Is known to say things. That is all.
Reaction score
280
We know that now. We didn't when you started the thread.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Well, it might help if we knew what it is supposed to do.
What it is currently doing and what should happen instead.

Though, replace that "index" count with:
set FOG = FirstOfGroup(PhysUnits)
exitwhen FOG == null
 

Dryvnt

New Member
Reaction score
10
Well what is supposed to do is that the unit should move, but it aint doing it :(

Try copy pasting it into a random map and see what happens for you.
 

Dryvnt

New Member
Reaction score
10
Bump... Lemme recap.

I got this system that moves units, currently I'm trying to make it move a non edited peasant, but it wont. I use the SetUnitX() and SetUnitY() functions to move the unit.
 

Builder Bob

Live free or don't
Reaction score
249
JASS:
call BJDebugMsg(R2S(X[GetUnitUserData(CurrentUnit[Index])])) //This is a debugger, the text shows.
            call BJDebugMsg(R2S(GetUnitX(CurrentUnit[Index]))) //Same as above
            call SetUnitX(CurrentUnit[Index], (GetUnitX(CurrentUnit[Index]) + X[Index]))
            call SetUnitY(CurrentUnit[Index], (GetUnitY(CurrentUnit[Index]) + Y[Index]))


You use debug messages to check
JASS:
X[GetUnitUserData(CurrentUnit[Index])]
GetUnitX(CurrentUnit[Index])

but you don't check
JASS:
X[Index]
Y[Index]


If that is not related to the problem, then this is my suggestion.

Try make a trigger that just uses GetUnitX/GetUnitY and nothing else.
If it still fails, disable your other triggers one by one to find where the error lies.
If it does move the unit, the problem must be somewhere else in the code you've posted. In which case you must widen your search and litter debug messages all over the place to check two things:
  1. Does all code run properly as you expect it to?
  2. Are all variables the value you expect them to be?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top