Casting a Ray with multiple Vectors?

Hi, I’m currently working on a 3D-BirdEyed style game.
One of the parts i’m currently working on includes changing the Camera if the Player is either behind a Part or if the Player is below a Part (Such as the roof of an interior) - Was wondering if this is doable through the use of a single ray instead of casting multiple rays?

    local Module =

    {RunService = game:GetService("RunService"),

    Camera = workspace:WaitForChild("Camera"),

    Player = game.Players.LocalPlayer,

    Offset = Vector3.new(0, 20, 20)}

    function Module.Load()

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

    end

    function onRenderStep()

    local Position = Module.Player.Character:WaitForChild("HumanoidRootPart").CFrame.p

    Module.Camera.CoordinateFrame = CFrame.new(Position + Module.Offset, Position)

    local CameraRay = Ray.new(Position, Position + Vector3.new(0, 500, 500) * 500)

    local CameraHit, position = workspace:FindPartOnRayWithIgnoreList(CameraRay, {Module.Player.Character})

    if CameraHit then

    if CameraHit.Name == "Roof" then

    Module.Offset = Vector3.new(0, 10, 20)

    elseif CameraHit.Name == "Building" then

    Module.Offset = Vector3.new(0, 20, 5)

    end

    else

    Module.Offset = Vector3.new(0, 20, 20)

    end

    end

    return Module

A much better method for this than racycasting is to use is GetPartsObscuringTarget

You can use multiple vectors with this method (though I’m not too sure why you’d want to), and it will save you a lot of code.

Example of how you would hide parts blocking the character:

local obscuringParts = {}
local function onRenderStep()
	for i, v in pairs(obscuringParts) do
		v.LocalTransparencyModifier = 0	
	end
	obscuringParts = workspace.CurrentCamera:GetPartsObscuringTarget({Player.Character.PrimaryPart.Position}, {Player.Character:GetChildren()})
	for i, v in pairs(obscuringParts) do
		v.LocalTransparencyModifier = 0.5
	end
end

Thanks to @AyeeAndrxw for pointing out that LocalTransparencyModifier existed.

6 Likes

You should probably be using LocalTransparency for this, as it won’t affect the old transparency of a part.

1 Like

Thanks but this doesn’t help with the Player being inside the interior (As this is a separate “chunk” loaded)
This is how the Camera itself will function, however upon a ‘roof’ in the Y axis or a ‘building’ in the Z access it will adjust.
548

You can use this method to find your camera offset, instead of using multiple raycasts and if starement. Just iterate through all the parts obscuring the view and add an offset for each one.

1 Like

You are completely right. I didn’t even know that existed, thank you. I will edit this now.