ChrisPBcn
ChrisPBcn

Reputation: 57

How to set camera height

I am trying to create a top down camera using a tutorial, here's the link: https://education.roblox.com/en-us/resources/arcade-game-top-down-camera

Here is my code:

local RunService = game:GetService("RunService")    

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local CAMERA_OFFSET = Vector3.new(-1,20,0)
camera.CameraType = Enum.CameraType.Scriptable

local function onRenderStep()
    if player.Character then
        local playerPosition = player.Character.HumanoidRootPart.Position
        local cameraPosition = playerPosition + CAMERA_OFFSET

        camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
        print("player position = " .. playerPosition.X .. " : " .. playerPosition.Y .. " : " .. playerPosition.Z)
        print("camera position = " .. cameraPosition.X .. " : " .. cameraPosition.Y .. " : " .. cameraPosition.Z)
    end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep) 

This set the camera above the player as expected. However, when I changed the value of CAMERA_OFFSET (for example change Y from 20 to 200), and restart the game, the camera did not change position cpompared to where it was with the previous value. Debugging the player and camera position, I can see that the offset was added correctly:

player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05

camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05

player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05

camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05

player position = 4.3593798181973e-05 : 2.8005499839783 : -1.193955540657e-05

camera position = -0.99995642900467 : 22.800550460815 : -1.193955540657e-05

My ultimate goal is to have the camera fixed above the player (right now its still possible to move it up and down with mouse scrolling), but first to understand how to set it in the right height.

Upvotes: 1

Views: 189

Answers (1)

Night94
Night94

Reputation: 975

If you put a

wait()

at the top of your script it will do exactly what you want.

I am not sure if that is the proper way to do it, or if you are supposed to wait for some other object to complete loading. There is no error message of any kind, and I couldn't find any documentation either.

Upvotes: 0

Related Questions