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

How do I prevent the camera from clipping through parts?

Asked by
rexhawk 222 Moderation Voter
4 years ago

Hello, I'm just going to go right into this...

recently I added a camera script into my first person game that makes the player's camera have an offset, the advantage to this is that it gives a good effect, but it has it's downside, which is that the player's camera can clip through the walls.

Example

Is there a way to prevent this? It's ruining the effect of my game and the only way I see a way of fixing this is to entirely remove the script, or add invisible parts along the walls to give a bigger hitbox.

Code I'm using (FM script I modified)

local camera = game:GetService("Workspace").CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local function setCameraOffset(operation)
    if operation == 1 then
        humanoid.CameraOffset = Vector3.new(0,0,-1.2)
    elseif operation == 2 then
        humanoid.CameraOffset = Vector3.new(0,-1,-1.2)
    elseif operation == 3 then
        humanoid.CameraOffset = Vector3.new(0,-3,-1.2)
    end
end

local function lock(part)
    if part and part:IsA("BasePart") then
        part.LocalTransparencyModifier = part.Transparency
        part.Changed:Connect(function(property)
            part.LocalTransparencyModifier = part.Transparency
        end)
    end
end

character.ChildAdded:Connect(lock)

while wait(1) do
    if humanoid.CameraOffset == Vector3.new(0,0,0) then
        setCameraOffset(1)
    elseif humanoid.CameraOffset == Vector3.new(0,0,-1.2) then
        setCameraOffset(1)
    elseif humanoid.CameraOffset == Vector3.new(0,-1,-1.2) then
        setCameraOffset(2)
    elseif humanoid.CameraOffset == Vector3.new(0,-3,-1.2) then
        setCameraOffset(3)
    end
    for _,v in pairs(character:GetChildren()) do
        lock(v)
    end
end
0
Probably you need to make second script that make the player cannot get more near than 1 stus from a wall ErtyPL 129 — 4y
0
1 stud* / elseif the player is getting very near to a wall let's the camera script just move the camera 0.5 stud away from the wall/walls ErtyPL 129 — 4y
0
How would I do that? Raycasting? rexhawk 222 — 4y
0
raycasting Fifkee 2017 — 4y

Answer this question