Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get this gui bar to center when looking at a player?

Asked by 4 years ago

I'm editing a FM compass script and am trying have the direction of a GUI bar be towards the player. So instead of having North, the target players location would be North. This should ultimately function as a tracker on another players location where the bar will be centered when looking in said players direction.

https://www.roblox.com/games/160136981/WIP

The green bar at the top should be centered when looking at the test dummy.

local smoothness = 30/3

wait(1)

local follow = game.Workspace.King
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

local dev = script.Parent

local lastY = 0

function restrictAngle(angle)
    if angle < -math.pi then
        return angle + math.pi*2
    elseif angle > math.pi then
        return angle - math.pi*2
    else
        return angle
    end
end

while true do
    local delta = wait(1/30)

    local look = camera.CoordinateFrame.lookVector
    local look = Vector3.new(look.x, 0, look.z).unit
    local lookY = math.atan2(look.z, look.x)

    local difY = restrictAngle(lookY - lastY)
    lookY = restrictAngle(lastY + difY*delta*smoothness)
    lastY = lookY

        playerLocation = math.pi * 4/4 -- RN this is north, how would I have the players torso location as north?
        bar = dev.Middle
        rot = restrictAngle(lookY - playerLocation)
        if math.sin(rot) > 0 then
            local cosRot = math.cos(rot)
            local cosRot2 = cosRot*cosRot

            bar.Visible = true
            bar.Position = UDim2.new(0.5 + cosRot*0.6, bar.Position.X.Offset, 0, 3)


        else
            bar.Visible = false
        end
    end

Answer this question