#Clipping through walls when using a custom camera

1 messages · Page 1 of 1 (latest)

jagged crystal
#

I've been making a custom camera and this code runs per frame:

RunService.RenderStepped:Connect(function()
    UserInputService.MouseIconEnabled = false
    if not char:FindFirstChild "UpperTorso" then return end
    
    local ray = cam:ViewportPointToRay(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2, 0.5)
    local dir = Vector3.new(ray.Direction.X, 0, ray.Direction.Z).Unit * FORWARD_OFFSET
    
    --[[local raycast = workspace:Raycast(char.UpperTorso.Position, ray.Direction.Unit * FORWARD_OFFSET, params)
    if raycast and raycast.Instance.Transparency < 1 then
        print(raycast.Instance.Name)
        dir = Vector3.new(0)
    end]]
    
    local rx, _, rz = char.UpperTorso.CFrame:ToOrientation() -- mild camera shake based on animation
    local _, ry2, _ = char.HumanoidRootPart.CFrame:ToOrientation() -- excepting the y-axis
    
    angley = lerpAngle(angley, ry2, LERP_SPEED) -- makes sure the angle doesn't accidentally wrap round and disorient the heck out of the player
    
    cam.CFrame = (CFrame.new(char.UpperTorso.Position) + dir) * (CFrame.Angles(rx*CAMERA_SHAKE, angley, rz*CAMERA_SHAKE)) * CFrame.new(0, 2, 0) * CFrame.Angles(angle, 0, 0)
    
    local d = UserInputService:GetMouseDelta()

    char:PivotTo(char.PrimaryPart.CFrame * CFrame.Angles(0, -math.rad(d.x), 0))
    target_angle = math.clamp(target_angle - math.rad(d.y), math.rad(-60), math.rad(60))
    angle = math.lerp(angle, target_angle, LERP_SPEED)
end)

as you can see, I've already tried to move the camera back when the player faces a wall (which has been commented out), however when looking too far up / down, the camera clips through anyway.

iron zenith
#

i suggest focusing on this cam.CFrame = (CFrame.new(char.UpperTorso.Position) + dir) ...ignore the rest, they're just angles
uppertorspo.position + dirXZ*FORWARD_OFFSET.
Perhaps start there

#

coz that don't seem right, with a large enough forward_offset yeah you probably could push the camera through walls, you have no checks for this.

jagged crystal
#

the only issue then is that it looks really weird when looking down as you can see a bit behind the torso

jagged crystal
#

ok i managed to get it fixed

#
local ray = cam:ViewportPointToRay(cam.ViewportSize.X / 2, cam.ViewportSize.Y / 2, 0.5)
    local dir = Vector3.new(ray.Direction.X, 0, ray.Direction.Z).Unit
    
    local raycast = workspace:Raycast(char.UpperTorso.Position, dir * FORWARD_OFFSET * 2, params)
    targetOffset = FORWARD_OFFSET
    if raycast and raycast.Instance.Transparency < 1 then
        targetOffset = FORWARD_OFFSET - (raycast.Position - char.UpperTorso.Position).Magnitude / 2 - 0.05
    end
    currentOffset = math.lerp(currentOffset, targetOffset, LERP_SPEED)