Why is this script for an isometric camera not working? Specifically, it throws an error at Line 76. I’m trying to make a raycast detection to make a part blocking the view of the player invisible.
I tried switching to LocalTransparencyModifier, but it didn’t work either.
--[[
Demonstrates how to update a player's camera to create an Isometric Camera style.
The camera updates every render step to ensure the camera maintains a constant distance from the player, as well as follows the player as they move.
--]]
task.wait(0.02)
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local InputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
-- Controls how far the camera is relative to the player's position
local CAMERA_DEPTH = 64
-- Controls the height above the player the camera will focus towards
local HEIGHT_OFFSET = 1
-- Field of view controls how much of the world the player can see
local FIELD_OF_VIEW = 15
local BLOCKED_PART = nil
local BLOCKED_PART_TRANSPARENCY = 0
local ROTATION = 1
TimeSinceRotate = 0
TimeSincePrint = 0
ZoomMode = "Out"
--[[
Runs every render step to ensure camera:
- maintains a constant distance from the player
- follows the player as they move
--]]
local function updateCamera()
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
if ZoomMode=="In" then
HEIGHT_OFFSET=0.5
else
HEIGHT_OFFSET=1
end
local playerPosition = humanoidRootPart.Position + Vector3.yAxis * HEIGHT_OFFSET
if ROTATION==1 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH,CAMERA_DEPTH,CAMERA_DEPTH)
elseif ROTATION==2 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH*1.333334,CAMERA_DEPTH,0)
elseif ROTATION==3 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH,CAMERA_DEPTH,-CAMERA_DEPTH)
elseif ROTATION==4 then
cameraPosition = playerPosition + Vector3.new(0,CAMERA_DEPTH,-CAMERA_DEPTH*1.333334)
elseif ROTATION==5 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH,CAMERA_DEPTH,-CAMERA_DEPTH)
elseif ROTATION==6 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH*1.333334,CAMERA_DEPTH,0)
elseif ROTATION==7 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH,CAMERA_DEPTH,CAMERA_DEPTH)
else
cameraPosition = playerPosition + Vector3.new(0,CAMERA_DEPTH,CAMERA_DEPTH*1.333334)
end
local Workspace = game:GetService("Workspace")
local rayOrigin = cameraPosition
local rayDirection = playerPosition-cameraPosition
local rayParams = RaycastParams.new()
rayParams.IgnoreWater=false
rayParams.RespectCanCollide=false
local raycastResult = Workspace:Raycast(rayOrigin, rayDirection, rayParams)
local hit = raycastResult.Instance
if hit~=nil then
if hit.Parent~=game.Players.LocalPlayer.Character then
if hit==BLOCKED_PART then
if BLOCKED_PART_TRANSPARENCY~=1 then
BLOCKED_PART_TRANSPARENCY=BLOCKED_PART.Transparency
end
BLOCKED_PART=hit
BLOCKED_PART.Transparency=1
else
BLOCKED_PART.Transparency=BLOCKED_PART_TRANSPARENCY
BLOCKED_PART=hit
end
end
else
if BLOCKED_PART~=nil then
BLOCKED_PART.Transparency=BLOCKED_PART_TRANSPARENCY
end
end
local playerPosition = humanoidRootPart.Position + Vector3.yAxis * HEIGHT_OFFSET
if ROTATION==1 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH,CAMERA_DEPTH,CAMERA_DEPTH)
elseif ROTATION==2 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH*1.333334,CAMERA_DEPTH,0)
elseif ROTATION==3 then
cameraPosition = playerPosition + Vector3.new(CAMERA_DEPTH,CAMERA_DEPTH,-CAMERA_DEPTH)
elseif ROTATION==4 then
cameraPosition = playerPosition + Vector3.new(0,CAMERA_DEPTH,-CAMERA_DEPTH*1.333334)
elseif ROTATION==5 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH,CAMERA_DEPTH,-CAMERA_DEPTH)
elseif ROTATION==6 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH*1.333334,CAMERA_DEPTH,0)
elseif ROTATION==7 then
cameraPosition = playerPosition + Vector3.new(-CAMERA_DEPTH,CAMERA_DEPTH,CAMERA_DEPTH)
else
cameraPosition = playerPosition + Vector3.new(0,CAMERA_DEPTH,CAMERA_DEPTH*1.333334)
end
camera.CFrame = CFrame.lookAt(cameraPosition, playerPosition)
end
end
end
-- Set the camera's field of view
camera.FieldOfView = FIELD_OF_VIEW
-- Change the camera type to Scriptable to not conflict with the default camera behavior
camera.CameraType = Enum.CameraType.Scriptable
-- Update the camera every render step
RunService.RenderStepped:Connect(updateCamera)
local mouse = player:GetMouse()
mouse.WheelForward:Connect(function()
game.SoundService.UI.CameraZoom:Play()
ZoomMode="In"
for i = 1, 8 do
CAMERA_DEPTH = math.clamp(CAMERA_DEPTH-0.5,16,128)
task.wait(0.01)
end
end)
mouse.WheelBackward:Connect(function()
game.SoundService.UI.CameraZoom:Play()
ZoomMode="Out"
for i = 1, 8 do
CAMERA_DEPTH = math.clamp(CAMERA_DEPTH+0.5,16,128)
task.wait(0.01)
end
end)
InputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if input.KeyCode==Enum.KeyCode.Right or input.KeyCode==Enum.KeyCode.DPadRight then
ROTATION=ROTATION+1
if ROTATION<=0 then
ROTATION=8
elseif ROTATION>=9 then
ROTATION=1
end
game.SoundService.UI.Click:Play()
elseif input.KeyCode==Enum.KeyCode.Left or input.KeyCode==Enum.KeyCode.DPadLeft then
ROTATION=ROTATION-1
if ROTATION<=0 then
ROTATION=8
elseif ROTATION>=9 then
ROTATION=1
end
game.SoundService.UI.Click:Play()
end
end)
InputService.TouchPinch:Connect(function(touchPositions: {any}, scale: number, velocity: number, state: Enum.UserInputState, gameProcessedEvent: boolean)
if scale>0 then
if scale<=1 then
ZoomMode="In"
game.SoundService.UI.CameraZoom:Play()
for i = 1, 2 do
CAMERA_DEPTH = math.clamp(CAMERA_DEPTH-0.5,16,128)
task.wait(0.01)
end
else
ZoomMode="Out"
game.SoundService.UI.CameraZoom:Play()
for i = 1, 2 do
CAMERA_DEPTH = math.clamp(CAMERA_DEPTH+0.5,16,128)
task.wait(0.01)
end
end
end
end)
task.wait(0.05)
local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("RozedsUi")
local ui = gui:FindFirstChild("Mobile")
if ui~=nil then
ui.ButtonRight.Activated:Connect(function(touchPositions: {any})
ROTATION=ROTATION+1
if ROTATION<=0 then
ROTATION=8
elseif ROTATION>=9 then
ROTATION=1
end
game.SoundService.UI.Click:Play()
end)
ui.ButtonLeft.Activated:Connect(function(touchPositions: {any})
ROTATION=ROTATION-1
if ROTATION<=0 then
ROTATION=8
elseif ROTATION>=9 then
ROTATION=1
end
game.SoundService.UI.Click:Play()
end)
end