So I was wondering how I could make the camera, for my top-down Side project, to shift towards the cursor, but not shift too much so it goes on for infinite or so.
local player = game.Players.LocalPlayer
local function UpdateCamera()
local char = player.Character
if not char then return end
local root = char:FindFirstChild("HumanoidRootPart")
if not root then return end
workspace.CurrentCamera.CFrame = CFrame.Angles(math.rad(-90), 0, 0) * CFrame.new(0, 0, 25) + root.Position
end
game:GetService("RunService"):BindToRenderStep("UpdateCamera", Enum.RenderPriority.Camera.Value + 1, UpdateCamera)
Not sure how I can provide an example, but what I mean is when your cursor goes to the side of the screen, the camera slightly shifts towards the cursor rather than staying on the player
Without a video and without any examples it’s really hard to tell what you’re trying to achieve. Could you maybe show different examples of how the camera should look depending on the cursor position?
you can also change the movement to match the camera.
Code
-- Edit variables --
local UpDistance = 25
local MaxDistance = 5
-- Service --
local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
-- Update --
local Camera = workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable
game:GetService("RunService").RenderStepped:Connect(function()
if not Player.Character then return end
-- Normal --
local Pivot = Player.Character:GetPivot()
local NewCamPosition = Pivot.Position+Vector3.yAxis*UpDistance
if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
-- Angle --
local P, O = UIS:GetMouseLocation(), Camera.ViewportSize/2
local Angle = -math.atan2(P.Y - O.Y, P.X - O.X)
local Distance = math.min((P-O).Magnitude/O.Y, 1)*MaxDistance
-- Set position --
local CF = (Pivot.Rotation+NewCamPosition)*CFrame.Angles(0, Angle, 0)
local Final = CF.Position-CF.LookVector*Distance
Camera.CFrame = CFrame.lookAt(Final, Final-Vector3.yAxis)
else
Camera.CFrame = CFrame.lookAt(NewCamPosition, Pivot.Position)
end
end)