I want to make the camera stay fixed until the player moves offsreen then move the camera to the player. Replies will be appreciated
Set the camera to Scriptable. Then find whatever offset from the HumanoidRootPart you want the camera to move to. Set the Camera’s CFrame at this point to
workspace.CurrentCamera.CFrame = HumanoidRootPart.CFrame * OFFSET_CFRAME
To detect if your character is on screen take a look at How do you check if a part is on screen?
in essence,
local _, withinScreenBounds = workspace.CurrentCamera:WorldToScreenPoint(HumanoidRootPart.Position)
if withinScreenBounds then
-- We have successfully detected if the player is on screen
end
Combine these two, and put it within .RenderStepped and you have your solution.
where do I add the camera? in the HumRootPart?
The Camera is an already existing object found at workspace.CurrentCamera in any local script.
I cant find scriptable in the cameras properties.(sorry about all the quetions I am new to scripting)
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
Place the script in the StarterPlayerScripts folder, local scripts will only execute if they are placed in the correct location. RenderStepped is an event of the RunService service, you can learn more about its application/usage here:
https://developer.roblox.com/en-us/api-reference/event/RunService/RenderStepped
sigh…
local HEIGHT = 25 -- height of the camera
local ALLOW_VIEW_BLOCKING = false -- whether or not objects can obstruct the view of the player
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootpart = character:WaitForChild("HumanoidRootPart")
local cameraPos
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {character, camera}
params.IgnoreWater = true
local function moveCamera()
local rootpos = rootpart.Position
local pos
if ALLOW_VIEW_BLOCKING then
pos = rootpos + Vector3.new(0,HEIGHT,0)
else
local result = workspace:Raycast(rootpos, Vector3.new(0,HEIGHT,0), params)
pos = result ~= nil and result.Position - Vector3.new(0,0.25,0) or rootpos + Vector3.new(0,HEIGHT,0)
end
camera.CFrame = CFrame.new(0,0,0,0,1,0,0,0,1,1,0,0) + pos
cameraPos = camera.CFrame.Position
end
moveCamera()
game:GetService("RunService").RenderStepped:Connect(function()
local _, withinScreenBounds = workspace.CurrentCamera:WorldToScreenPoint(rootpart.Position)
local rootpos = rootpart.Position
local cameraBlockedCast = (ALLOW_VIEW_BLOCKING == false and workspace:Raycast(rootpos, cameraPos - rootpos, params)) or nil
if not withinScreenBounds or cameraBlockedCast ~= nil then
moveCamera()
end
end)
place in StarterCharacterScripts
@Scarious The camera is stuck on top view. which part of the script view do I change to make the camera go behind?