I currently have a script that makes the camera wobble when you walk. I love the wobble but it can clip through the walls and I’m not sure how I could prevent it. Any help would be appreciated! The script and example is attached below.
RunService.RenderStepped:Connect(function()
local CT = tick()
if StaminaAmount >=99 then
BarBack.Visible = false
end
if StaminaAmount <= 25 then
blur.Enabled = true
if not SoundTired.IsPlaying then SoundTired.Volume = 1 SoundTired:Play() end
else
blur.Enabled = false
SoundTired.Volume -= .01
if SoundTired.Volume <= 0 then SoundTired:Stop() end
end
if usingStam == false and StaminaAmount > 49 and StaminaAmount < 100 then
print("No use")
StaminaAmount += (STAMINA_INCREASE-.1)
BarBack.Visible = true
ControlBar:TweenSize(UDim2.new(StaminaAmount / 100,0,1,0), "Out", "Linear", 0)
end
if StaminaAmount >= 50 then
StaminaClosed = false
ControlBar.BackgroundColor3 = Color3.fromHex("#ffffff")
elseif StaminaClosed and StaminaAmount < 50 then
usingStam = false
StaminaAmount += (STAMINA_INCREASE-.1)
ControlBar.BackgroundColor3 = Color3.new(1, 0, 0)
BarBack.Visible = true
ControlBar:TweenSize(UDim2.new(StaminaAmount / 100,0,1,0), "Out", "Linear", 0)
end
if humanoid.MoveDirection.Magnitude > 0 then
local BobbleX
local BobbleY
if Running and StaminaAmount > 1 then
BobbleX = math.cos(CT*10)*1.3
BobbleY = math.abs(math.sin(CT*10))*1.3
else
BobbleX = math.cos(CT*10)*0.25
BobbleY = math.abs(math.sin(CT*10))*0.25
end
local Bobble = Vector3.new(BobbleX,BobbleY,0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(Bobble, 0.25)
else
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end
end)
Hello, I think this might help you, please understand that It might not work for you since this code is not tested or optimized
The issue with your camera clipping through walls is likely related to the fact that it is wobbling in a way that doesn’t account for the environment around it. The “Bobble” value is used to set the camera offset directly, which can result in the camera moving into areas where it should not, such as inside a wall.
A possible solution to this is to use raycasting. You can use raycasting to check if there’s a wall (or any obstacle) between the camera and the player character. If there is, you can adjust the camera offset or position to prevent it from moving into the wall. Here’s an example of how you might modify your code to include a raycast check. Please note that this is a simple example and might need adjustments according to your specific game setup and requirements. Also, this assumes that you have workspace available (which is a common variable in Roblox):
if humanoid.MoveDirection.Magnitude > 0 then
local BobbleX
local BobbleY
if Running and StaminaAmount > 1 then
BobbleX = math.cos(CT*10)*1.3
BobbleY = math.abs(math.sin(CT*10))*1.3
else
BobbleX = math.cos(CT*10)*0.25
BobbleY = math.abs(math.sin(CT*10))*0.25
end
local Bobble = Vector3.new(BobbleX,BobbleY,0)
local nextCameraOffset = humanoid.CameraOffset:lerp(Bobble, 0.25)
-- Define a ray from player position to the next camera offset
local ray = Ray.new(humanoid.RootPart.Position, nextCameraOffset - humanoid.CameraOffset)
-- Check if ray hits any obstacle
local hit, hitPosition = workspace:FindPartOnRay(ray, humanoid)
if hit then
-- Adjust camera offset if there's a wall between player and camera
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
else
humanoid.CameraOffset = nextCameraOffset
end
else
humanoid.CameraOffset = humanoid.CameraOffset * 0.75
end