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

[Solved] [Lua] Keeping a unit on a Terrain

Status
Not open for further replies.
Level 8
Joined
Jun 16, 2008
Messages
333
I am moving my units but I would like for them to stay on lava but they glitch out with the code I have.
If anyone knows an easy way about this I would really appreciate it.

my code
Lua:
function pandaMoveTrig()
    xpcall(function()
    _G.pandaArray = {}
    _G.pandaTimer = CreateTimer()
    TimerStart(_G.pandaTimer, 0.02, true, pandaMoveCon)
end, print)
end

function pandaMoveCon()
    local panda = GetUnitsOfTypeIdAll(FourCC("h003"))
    ForGroup(panda, pandaMove)
    DestroyGroup(panda)
end 

function pandaMove()

    local unit = GetEnumUnit()
    local x = GetUnitX(unit)
    local y = GetUnitY(unit)
    local p = GetUnitLoc(unit)
    local onTerrain = GetTerrainType(x, y)
    local lavaTerrain = FourCC("Dlvc")
    local Speed = 8.00
    local f = GetUnitFacing(unit) - 180
    print(_G.pandaArray[unit])
    if onTerrain == lavaTerrain then
        SetUnitPositionLoc(unit, PolarProjectionBJ(GetUnitLoc(unit),Speed,GetUnitFacing(unit)))
        _G.pandaArray[unit] = 0
    end
    if onTerrain ~= lavaTerrain and _G.pandaArray[unit] == 0 then
        SetUnitFacing(unit, math.random(f-5, f+5))
        _G.pandaArray[unit] = 1
    end
    if onTerrain ~= lavaTerrain and _G.pandaArray[unit] == 1 then
        SetUnitPositionLoc(unit, PolarProjectionBJ(GetUnitLoc(unit),Speed,GetUnitFacing(unit)))
    end
end

panda.png

I also tried this but it doesn't work either
Code:
function pandaMoveTrig()
    xpcall(function()
    _G.pandaArray = {}
    _G.pandaTimer = CreateTimer()
    TimerStart(_G.pandaTimer, 0.02, true, pandaMoveCon)
end, print)
end

function pandaMoveCon()
    local panda = GetUnitsOfTypeIdAll(FourCC("h003"))
    ForGroup(panda, pandaMove)
    DestroyGroup(panda)
end  

function pandaMove()

    local unit = GetEnumUnit()
    local x = GetUnitX(unit)
    local y = GetUnitY(unit)
    local p = GetUnitLoc(unit)
    local f = GetUnitFacing(unit)
    local onTerrain = GetTerrainType(x, y)
    local southTerrain = GetTerrainType(x, y - 1)
    local northTerrain = GetTerrainType(x, y + 1)
    local eastTerrain = GetTerrainType(x + 1, y)
    local westTerrain = GetTerrainType(x - 1, y)
    local lavaTerrain = FourCC("Dlvc")
    local Speed = 8.00
    if f <= 316 and f >= 43 and northTerrain ~= lavaTerrain then --north
            setUnitFacing(unit, f - 180)
    end
    if f <= 44 and f >= 135 and eastTerrain ~= lavaTerrain then --east
        setUnitFacing(unit, f - 180)
    end
    if f <= 136 and f >= 225 and westTerrain ~= lavaTerrain then --south
        setUnitFacing(unit, f - 180)
    end
    if f <= 226 and f >= 315 and southTerrain ~= lavaTerrain then --west
        setUnitFacing(unit, f - 180)
    end
    if onTerrain == lavaTerrain then
        SetUnitPositionLoc(unit, PolarProjectionBJ(GetUnitLoc(unit),Speed,GetUnitFacing(unit)))
    end

end

So I figured it out with a little help from discord. PolarProjectionBJ is what I needed.

function pandaMoveTrig()
xpcall(function()
_G.pandaArray = {}
_G.pandaTimer = CreateTimer()
TimerStart(_G.pandaTimer, 0.02, true, pandaMoveCon)
end, print)
end

function pandaMoveCon()
local panda = GetUnitsOfTypeIdAll(FourCC("h003"))
ForGroup(panda, pandaMove)
DestroyGroup(panda)
end

function pandaMove()

local unit = GetEnumUnit()
local x = GetUnitX(unit)
local y = GetUnitY(unit)
local p = GetUnitLoc(unit)
local f = GetUnitFacing(unit)
local d = 90 --Distance for projection
local onTerrain = GetTerrainType(x, y)
local forwardTerrain = GetTerrainType(GetLocationX(PolarProjectionBJ(p, d, f)), GetLocationY(PolarProjectionBJ(p, d, f)))
local lavaTerrain = FourCC("Dlvc")
local Speed = 8.00
local fTony = f - math.random( 170, 190 )
print("RUNNNN")
print(unit)
print(f)

if forwardTerrain ~= lavaTerrain then
BlzSetUnitFacingEx(unit, fTony)
print("a" .. northTerrain .. "a")
end
if onTerrain == lavaTerrain then
SetUnitPositionLoc(unit, PolarProjectionBJ(p,Speed,f))
end
end
 
Last edited:
Status
Not open for further replies.
Top