- What do you want to achieve?
I would like to stop the camera from snaping to the mouse, instead I want it to drag from there.
- What is the issue?
It snaps to the mouse position as shown in video when its activated.
robloxapp-20230326-1233226.wmv (849.2 KB)
-
What solutions have you tried so far?
I have tried many other things that have not worked, including checking what 3D position the mouse is in and doing some math with lerp.
Script that moves camera with mouse
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local oldpos = nil
local rdown = false
local back = true
local camera = Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local oldMousex = game.Players.LocalPlayer:GetMouse().X
local oldMousey = game.Players.LocalPlayer:GetMouse().Y
local Mousex
local Mousey
local lastlerpx = 0
local lastlerpy = 0
local camera = game.Workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
game:GetService("RunService").RenderStepped:Connect(function()
camera = Workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
if rdown == true then
Mousex = game.Players.LocalPlayer:GetMouse().X
Mousey = game.Players.LocalPlayer:GetMouse().Y
if oldMousex == nil or oldMousey == nil then
oldMousex = Mousex
oldMousey = Mousey
warn('Old Mouse X or Old Mouse Y is nil.')
end
local maxTilt = 10
local dx = ( (mouse.X )- mouse.ViewSizeX / 2) / mouse.ViewSizeX
local dy = ( (mouse.Y ) - mouse.ViewSizeY / 2) / mouse.ViewSizeY
if math.abs(dx) > 0 or math.abs(dy) > 0 then
local tiltX = math.rad(dx * -maxTilt)
local tiltY = math.rad(dy * -maxTilt)
local newRotation = CFrame.Angles(tiltY, tiltX, 0)
camera.CFrame = camera.CFrame * newRotation
end
lastlerpy = (oldMousey - Mousey) / 100
lastlerpx = (oldMousex - Mousex) / 100
oldMousex = Mousex
oldMousey = Mousey
else
if back == false then
back = true
end
end
end)
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- right mouse down
print("Right mouse button down")
oldpos = camera.CFrame
rdown = true
end
end
local function onInputEnded(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton2 then -- right mouse up
print("Right mouse button up")
rdown = false
end
end
UserInputService.InputBegan:Connect(onInputBegan)
UserInputService.InputEnded:Connect(onInputEnded)
This is the second script that does not need to be edited but needed in order for the other script to work!
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local CAMERA_DEPTH = 64
local HEIGHT_OFFSET = 2
camera.FieldOfView = 20
camera.CameraType = "Scriptable"
local mouse = game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")
UIS.InputChanged:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseWheel then -- Check if the user scrolled the mouse wheel.
local Rotation = Input.Position.Z -- The Z property of the position indicates whether it moves forward or backwards, 1 being forward and -1 being backwards.
if Rotation == -1 then
if CAMERA_DEPTH < 200 then
--local newcamera = CAMERA_DEPTH + 5
--TS:Create(CAMERA_DEPTH, TweenInfo.new(1), {CAMERA_DEPTH = newcamera}):Play()
CAMERA_DEPTH = CAMERA_DEPTH + 5
end
--print(CAMERA_DEPTH)
else
if CAMERA_DEPTH > 32 then
--local newcamera2 = CAMERA_DEPTH - 5
--TS:Create(CAMERA_DEPTH, TweenInfo.new(1), {CAMERA_DEPTH = newcamera2}):Play()
CAMERA_DEPTH = CAMERA_DEPTH - 5
end
--print(CAMERA_DEPTH)
end
end
end)
local function updateCamera()
local character = player.Character
if character then
local root = character:FindFirstChild("HumanoidRootPart")
if root then
local rootPosition = root.Position + Vector3.new(0, HEIGHT_OFFSET, 0)
local cameraPosition = rootPosition + Vector3.new(CAMERA_DEPTH, CAMERA_DEPTH, CAMERA_DEPTH)
camera.CFrame = CFrame.lookAt(cameraPosition, rootPosition)
end
end
end
RunService:BindToRenderStep("IsometricCamera", Enum.RenderPriority.Camera.Value + 1, updateCamera)
Both scripts go in StarterGui as a local script, all replies are greatly appreciated!