Lua · Examples

Vector

Positions are tables {x, y, z} in game units. The vec helpers do the maths.

Each snippet runs as-is: copy it, press From Clipboard on the Lua page, turn it on. Quick start

#Distance to every enemy

Game units: about 1 unit = 1 inch, so divide by 39.37 for metres.

vector_1.lua
function on_frame()
    local me = game.local_player()
    if not me then return end
    local y = 200
    for _, p in ipairs(game.players()) do
        if p.enemy then
            local m = vec.distance(me.pos, p.pos) / 39.37
            draw.text(20, y, string.format("%s  %.0f m", p.name, m), color(230, 230, 230), 14)
            y = y + 18
        end
    end
end

#World to screen

Turn a world point into pixels. Skip it when the point is behind you.

vector_2.lua
local white = color(255, 255, 255)

function on_frame()
    for _, p in ipairs(game.players()) do
        if p.enemy then
            local x, y, ok = draw.world_to_screen(p.pos.x, p.pos.y, p.pos.z)
            if ok then draw.circle_filled(x, y, 3, white) end
        end
    end
end

#Direction and angles

vec.normalize gives a direction; math.angle_to gives the view angles that look at a point.

vector_3.lua
local me = entity.local_player()
local b = bomb.state()
if me and b.pos then
    local dir = vec.normalize(vec.sub(b.pos, me.pos))
    local pitch, yaw = math.angle_to(me.pos, b.pos)
    print("bomb direction", dir.x, dir.y, "yaw", yaw)
end

#A point in front of you

Turn your yaw into a vector and step forward.

vector_4.lua
function on_frame()
    local me = entity.local_player()
    if not me or not me.view_angles then return end
    local yaw = math.rad(me.view_angles.yaw)
    local ahead = vec.add(me.pos, vec.new(math.cos(yaw) * 200, math.sin(yaw) * 200, 0))
    draw.circle3d(ahead, 20, color(120, 200, 255), 2)
end