Hey folks, another problem has risen from this script I’m working on.
What I’m trying to do is to allow the camera to move without holding the right mouse button, since this is meant for an OTS gun system.
Problem: The camera seems to jolt when this happens and whenever I equip the tool.
I’m a little new to a few concepts here, so I feel like I may be missing a key component.
Here’s the code:
UserInputService.InputChanged:Connect(function(Input, GameProcessed) --For mouse being moved.
if GameProcessed then return end
if Input.UserInputType ~= Enum.UserInputType.MouseMovement then return end
local Delta = Input.Delta
AngleX = AngleX - Delta.X * .4
AngleY = math.clamp(AngleY - Delta.Y * .4, -80, 80)
end)
Tool.Equipped:Connect(function()
if Equipped == true then Character.Humanoid:UnequipTools() return end --Ensure someone isn't trying to break the script.
Equipped = true
local ArchivedCFrame = Camera.CFrame
RunService:BindToRenderStep("MoveWithHead", Enum.RenderPriority.Camera.Value, function()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
local StartingCFrame = NewCFrame(HumanoidRootPart.CFrame.p + Vector3.new(0, 1.5, 0)) * NewCFrameAngle(0, math.rad(AngleX), 0) * NewCFrameAngle(math.rad(AngleY), 0, 0)
local CameraCFrame = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3.new(0, 0, (Camera.CFrame.Position - Camera.Focus.Position).Magnitude))
local CameraFocus = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3.new(0, 0, -50000))
Camera.CFrame = NewCFrame(CameraCFrame.p, CameraFocus.p)
end)
end)
I can’t record a video, so here’s two still images.
Before I hold out the tool:
The amount of frustration I just experienced after realizing that was the solution is immeasurable. I was working with lerps the other day, not sure why that didn’t come to me before. Thank you so much.
New problem that sort of falls under the same category of “Camera Jolting”. I’m trying to lerp the camera from the OTS camera position to the Player’s regular camera without sacrificing zoom distance.
Problem is, the camera jolts when it reverts back (Probably miscalculating something).
However, I also face this problem when the character is moving for both animations.
I’ll attach an experience here with the tool and the rig I’m using, since I’m not sure how to solve this issue.
Edit: Removed after fix.
The jolting while moving is probably due to the Lerp calculation happening between the current camera position at the time of the switch, the time it takes to Lerp, and the position of the player after that time has passed while they are moving.
I’m not familiar with these calculations, but is it possible to make the Lerp constantly update to the players current position while they’re moving?
Actually yeah, I believe that IS what is happening, since it’s binded to render step. I could be wrong though.
RunService:BindToRenderStep("FixCamera", Enum.RenderPriority.Camera.Value, function()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter --To replicate a similar system like ShiftLock.
local StartingCFrame = NewCFrame(HumanoidRootPart.CFrame.p + Vector3D(0, 1.5, 0)) * NewCFrameAngle(0, Rad(AngleX), 0) * NewCFrameAngle(Rad(AngleY), 0, 0)
local CameraCFrame = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3D(0, 0, Floor((Camera.CFrame.Position - Camera.Focus.Position).Magnitude) - CameraOffset.Z/2)) --All three of these are just calculations. Info will be at the bottom.
local CameraFocus = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3D(0, 0, -50000))
Camera.CFrame = Camera.CFrame:Lerp(NewCFrame(CameraCFrame.p, CameraFocus.p), 0.35)
end)
Not really my area of expertise though, but you’re going to have to figure out how to make the .35 second Lerp work smoothly you may want to check out this section of RunService | Roblox Creator Documentation Note: All rendering updates will wait until the code in the render step finishes. Make sure that any code called by BindToRenderStep runs quickly and efficiently. If code in BindToRenderStep takes too long, then the game visuals will be choppy.
RunService:BindToRenderStep("FixCamera", Enum.RenderPriority.Camera.Value, function()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter --To replicate a similar system like ShiftLock.
local StartingCFrame = NewCFrame(HumanoidRootPart.CFrame.p + Vector3D(0, 1.5, 0)) * NewCFrameAngle(0, Rad(AngleX), 0) * NewCFrameAngle(Rad(AngleY), 0, 0)
local CameraCFrame = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3D(0, 0, CameraOffset.Z + (SavedPreOffset - CameraOffset.Z)))--+ (Abs(CameraOffset.Z - (Camera.CFrame.Position - Camera.Focus.Position).Magnitude - SavedPreOffset)))) --All three of these are just calculations. Info will be at the bottom.
local CameraFocus = StartingCFrame + StartingCFrame:VectorToWorldSpace(Vector3D(0, 0, -50000))
Camera.CFrame = Camera.CFrame:Lerp(NewCFrame(CameraCFrame.p, CameraFocus.p), 0.45) --Lerps camera to new camera position.
end)
I save the old value before I do anything, then when I calculate zooming in and out for the system (since your character can’t zoom normally in this mode), I do the same thing to the offset.
ConnectionAlpha = Mouse.WheelForward:Connect(function()
if CameraOffset.Z > 3 then
CameraOffset = Vector3D(CameraOffset.X, CameraOffset.Y, CameraOffset.Z - 3)
SavedPreOffset -= 3
end
end)
ConnectionBeta = Mouse.WheelBackward:Connect(function()
if CameraOffset.Z < 17 then
CameraOffset = Vector3D(CameraOffset.X, CameraOffset.Y, CameraOffset.Z + 3)
SavedPreOffset += 3
end
end)
I’ll keep your solution up in order to make sure the original topic is understood clearly.