I need some help with my script for a camera system. The attached video showcases what the script currently does. I will attach another video showing a more accurate representation of what I’m after by altering the code slightly, however my desired result is a blend of the two which I will explain.
The video above shows the result of the current code which is as follows:
-- Services
local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local rs = game:GetService("RunService")
local context = game:GetService("ContextActionService")
-- Player & Character
local plr = game:GetService("Players").LocalPlayer
local c = plr.Character or plr.CharacterAdded:Wait()
-- Other
local Camera = game:GetService("Workspace").CurrentCamera
local mouse = plr:GetMouse()
-- Values
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3, -1, 5)
local isWalking = true
-- CAMERA SETTING --
wait(0.01)
Camera.CameraType = Enum.CameraType.Scriptable
-- Function to update camera
local function updateCamera()
local rootPart = c:FindFirstChild("HumanoidRootPart")
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
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 targetCameraPos = isWalking and Vector3.new(cameraPos.X, cameraPos.Y, cameraPos.Z + 0.5) or cameraPos
local cameraCFrame = startCFrame + startCFrame:VectorToWorldSpace(targetCameraPos)
local cameraFocus = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(targetCameraPos.X, targetCameraPos.Y, -50000))
-- No Tween version
--Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
-- Tween the camera position
ts:Create(Camera, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = CFrame.new(cameraCFrame.p, cameraFocus.p)}):Play()
end
end
-- Bind action to handle mouse movement
context:BindAction("CameraMovement", function(_, _, input)
xAngle = xAngle - input.Delta.x * 0.4
yAngle = math.clamp(yAngle - input.Delta.y * 0.4, -80, 80)
end, false, Enum.UserInputType.MouseMovement)
-- Connect to RenderStepped to update camera
rs.RenderStepped:Connect(function()
updateCamera()
end)
-- Connect to MoveDirection property to determine if the character is walking
c.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if c.Humanoid.MoveDirection.Magnitude > 0 then
isWalking = true
else
isWalking = false
end
end)
This next video shows the closer representation of what I’m after by swapping out the tween line in the code above with the “no tween version”:
What I’m looking for is very similar to this video but with a few more subtle differences. As you can see from the video above, when the player begins moving, the camera is smoothly tweened backwards and back to its “original position” when the player stops moving. I want to create this effect but with the general camera movement that is shown in the second video
And now that i view it, if theres a way to make it less janky, as you can see in the second video it seems to “stutter” that would also be greatly appreciated