I’m trying to create a camera that moves based on WASD movement on the X and Y axis, it’s already working perfectly, but I recently wanted to add a zoom-in and out feature using the MouseWheel. So when you scroll it will change the Z value of the camera, with boundaries of course.
I’m getting the error “MouseWheelDelta is not a part of UserInputService” which I have recognized by now, but I can’t find an alternative solution. Could someone help me?
Code:
local Player = game.Players.LocalPlayer
Player.Character.HumanoidRootPart.Anchored = true
local TweenService = game:GetService("TweenService")
-- Get the camera and user input service
local camera = game.Workspace.CurrentCamera
local userInputService = game:GetService("UserInputService")
camera.CameraType = Enum.CameraType.Scriptable
-- Set the camera's CFrame to the starting position
camera.CFrame = CFrame.new(Vector3.new(-81.237, 63.212, 47.407))
-- Define the movement speed, rotation speed, and TweenInfo for camera movement
local movementSpeed = 10
local rotationSpeed = 1
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)
-- Create variables to track the user's input
local isMovingForward, isMovingBackward, isMovingLeft, isMovingRight = false, false, false, false
local mouseWheelDelta = 0
-- Connect a function to the user input service's InputBegan and InputEnded events
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.W then
isMovingForward = true
elseif input.KeyCode == Enum.KeyCode.A then
isMovingLeft = true
elseif input.KeyCode == Enum.KeyCode.S then
isMovingBackward = true
elseif input.KeyCode == Enum.KeyCode.D then
isMovingRight = true
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.W then
isMovingForward = false
elseif input.KeyCode == Enum.KeyCode.A then
isMovingLeft = false
elseif input.KeyCode == Enum.KeyCode.S then
isMovingBackward = false
elseif input.KeyCode == Enum.KeyCode.D then
isMovingRight = false
end
end)
-- Connect a function to the user input service's InputChanged event to track mouse wheel delta
userInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseWheel then
mouseWheelDelta = input.Position.Z
end
end)
while true do
-- Get the user's input and calculate the movement vector
local cameraOrientation = camera.CFrame
local moveVector = Vector3.new()
if isMovingForward then
moveVector = moveVector + (cameraOrientation.LookVector * Vector3.new(1, 1, 0) * movementSpeed)
moveVector = moveVector + (cameraOrientation.UpVector * movementSpeed)
end
if isMovingBackward then
moveVector = moveVector - (cameraOrientation.LookVector * Vector3.new(1, 1, 0) * movementSpeed)
moveVector = moveVector - (cameraOrientation.UpVector * movementSpeed)
end
if isMovingLeft then
moveVector = moveVector - (cameraOrientation.RightVector * Vector3.new(1, 1, 0) * movementSpeed)
end
if isMovingRight then
moveVector = moveVector + (cameraOrientation.RightVector * Vector3.new(1, 1, 0) * movementSpeed)
end
-- Add Z-axis movement when scrolling the mouse wheel
local mouseDelta = userInputService.MouseWheelDelta
if mouseDelta ~= 0 then
moveVector = moveVector + (cameraOrientation.LookVector * mouseDelta * 0.1)
end
-- Get the new position of the camera by adding the movement vector to the current position
local newPosition = camera.CFrame.Position + moveVector
-- Clamp the new position within the boundary limits
newPosition = Vector3.new(
math.clamp(newPosition.X, -145.757, -16.236),
math.clamp(newPosition.Y, 26.644, 99.614),
math.clamp(newPosition.Z, 47.407, 99.232)
)
-- Use TweenService to smoothly move the camera to the new position
local newCFrame = CFrame.new(newPosition, newPosition - Vector3.new(0, 0, 1))
TweenService:Create(camera, tweenInfo, { CFrame = newCFrame }):Play()
-- Wait for the next frame
wait()
end
Anything is appreciated! 