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

Moving camera along z-axis using camera bounding box?

Asked by 3 years ago
Edited 3 years ago

I'm trying to make a top-down style game where the camera moves once the player reaches certain points along the x and z axis.

I've got it moving just fine along the x axis, but i can't seem to get it to move along the z axis (forward/backward)

(video of the script below in action) https://gyazo.com/d85eb99bed810182cfdc5dae12d906e7

local function onRender(deltaTime) -- hooked up to runservice.renderstepped
    local lookAt = humanoidRootPart.CFrame.p
    local cameraLook = camera.CFrame.p
    local target = Vector3.new(0,0,0)

    local dx = lookAt.X - cameraLook.X;
    if dx > boundX or dx < -boundX then
        if cameraLook.X < lookAt.X then
            target = Vector3.new(dx - boundX, target.Y, target.Z)
        else
            target = Vector3.new(dx + boundX, target.Y, target.Z)
        end
    end

    --local dz blah blah blah math junk

    local desiredPosition = Vector3.new(cameraLook.X, cameraLook.Y, 0) + target
    desiredPosition = Vector3.new(desiredPosition.X, lookAt.Y, desiredPosition.Z)
    desiredPosition = CFrame.new(desiredPosition) * CFrame.fromOrientation(math.rad(-cameraAngle),0,0) * CFrame.new(0,0,cameraDistance)

    camera.CFrame = camera.CFrame:Lerp(desiredPosition, cameraSpeed*deltaTime)
end

I'm aware it's probably not the best code. I'm just trying to get something working before I refactor it and such.

Can someone help me figure out what I should do to get the camera moving properly?

heres the variables i have set

local cameraDistance = 50
local cameraAngle = 30
local cameraSpeed = 5

local boundX = 5
local boundZ = 20

Answer this question