I’m trying to create a shoulder camera that has a slight bobble effect to it to simulate walking. I’ve got it almost working but I’m struggling figuring out how to transition the camera between its walking state and stopping state.
rs.RenderStepped:Connect(function()
-- CHAR VARIABLES --
local c = plr.Character or plr.CharacterAdded:Wait()
local rootPart = c:FindFirstChild("HumanoidRootPart")
local humanoid = c:FindFirstChild("Humanoid")
local velocity = humanoid.RootPart.Velocity
-- BOBBLE VARIABLES --
local currentTime = tick()
local bobbleX = math.cos(currentTime * 10) * .35
local bobbleY = math.abs(math.sin(currentTime * 10)) * .35
local bobble = Vector3.new(bobbleX, bobbleY, 0) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
-- CAMERA VARIABLES --
if c and rootPart then
local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
local cameraCFrame = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X + 3,cameraPos.Y,cameraPos.Z - 2))
local cameraFocus = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
local lerpCamCFrame = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X + 3,cameraPos.Y,cameraPos.Z - 2):lerp(bobble, .25))
if humanoid.MoveDirection.Magnitude > 0 then
Camera.CFrame = CFrame.new(lerpCamCFrame.p, cameraFocus.p)
-- this is perfectly positioned
else
-- problem state / different positioning from the lerped cam
Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
end
end
end)
I am open to other suggestions on how to make this work (tweening, switching back to using humanoid.CameraOffset over using the CurrentCamera, etc) if it turns out I’m making this more complicated than it needs to be.
*I know if I was using humanoid.CameraOffset I know lerping would be sooooo much easier to restore the state, I’d just like to be able to physically position the camera