PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Delta

InputObject

Vector3

A Vector3 describing the Delta (change) between mouse/joystick movements.

This is useful when used with the input’s InputObject/Position|position to track the position and movement of the user’s mouse/joystick, such as when you’re creating custom movement or camera scripts. Consider tracking input object changes using the Instance/Changed event or when user input changes via events such as UserInputService/InputChanged and GuiObject/InputChanged.

See also

  • InputObject/KeyCode
  • InputObject/Position
  • InputObject/UserInputState
  • InputObject/UserInputType


Code Samples


Handling InputChanged

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

-- In order to use the InputChanged event, the UserInputService service must be used
local userInputService = game:GetService("UserInputService")

-- Prints the current input position and the change (delta) in position
local function printMovement(input)
    print("\tPosition:",input.Position)
    print("\tMovement Delta:",input.Delta)
end
 
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
   	 print("The mouse has been moved!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.MouseWheel then
   	 print("The mouse wheel has been scrolled!")
   	 print("\tWheel Movement:",input.Position.Z)
    elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
   	 if input.KeyCode == Enum.KeyCode.Thumbstick1 then
   		 print("The left thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
   		 print("The right thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
   		 print("The pressure being applied to the left trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
   		 print("The pressure being applied to the right trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 end
    elseif input.UserInputType == Enum.UserInputType.Touch then
   	 print("The user's finger is moving on the screen!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.Gyro then
   	 local rotInput,rotCFrame = UserInputService:GetDeviceRotation()
   	 local rotX,rotY,rotZ = rotCFrame:toEulerAnglesXYZ()
   	 local rot = Vector3.new(math.deg(rotX),math.deg(rotY),math.deg(rotZ))
   	 print("The rotation of the user's mobile device has been changed!")
   	 print("\tPosition",rotCFrame.p)
   	 print("\tRotation:",rot)
    elseif input.UserInputType == Enum.UserInputType.Accelerometer then
   	 print("The acceleration of the user's mobile device has been changed!")
   	 printMovement(input)
    end    
end

userInputService.InputChanged:Connect(InputChanged)

Create a Binoculars Script

This example creates a binoculars script that decreases the player’s Camera/FieldOfView|FieldOfView and UserInputService/MouseDeltaSensitivity|MouseDeltaSensitivity when a player with a UserInputService/MouseEnabled|MouseEnabled left mouse clicks. The script also points the player’s Camera towards the DataType/Vector3|Vector3 world position of the mouse click determined by the Mouse|Mouse’s Mouse/Hit|Mouse.Hit.p property.

When the player left mouse clicks again, the player’s camera reverts back to the a custom Enum/CameraType|CameraType with the same field of view and Camera/CFrame|CFrame as before the player zoomed in with the script.

While the player uses the binoculars, the script locks the player’s mouse to the center of the screen by setting the player’s UserInputType/MouseBehavior|MouseBehavior to LockCenter. The player’s camera moves when the player moves their mouse according to the InputObject/Delta|InputObject.Delta property passed by UserInputService/InputChanged|InputChanged indicating the mouse’s DataType/Vector2|Vector2 change in screen position.

In order for this example to work as expected, it should be placed in a LocalScript.

local UserInputService = game:GetService("UserInputService")

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)

local mouse = player:GetMouse()

local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties =
{
	FieldOfView = nil,
	_CFrame = nil,
	MouseBehavior = nil,
	MouseDeltaSensitivity = nil
}

local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0

-- Reset camera back to CFrame and FieldOfView before zoom
local function ResetCamera()
	target = nil
	camera.CameraType = Enum.CameraType.Custom
	camera.CFrame = originalProperties._CFrame
	camera.FieldOfView = originalProperties.FieldOfView
	
	UserInputService.MouseBehavior = originalProperties.MouseBehavior
	UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end

local function ZoomCamera()
	-- Allow camera to be changed by script
	camera.CameraType = Enum.CameraType.Scriptable
		
	-- Store camera properties before zoom
	originalProperties._CFrame = camera.CFrame
	originalProperties.FieldOfView = camera.FieldOfView
	originalProperties.MouseBehavior = UserInputService.MouseBehavior
	originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity

	-- Zoom camera
	target = mouse.Hit.p
	local eyesight = head.Position
	camera.CFrame = CFrame.new(eyesight, target)
	camera.Focus = CFrame.new(target)
	camera.FieldOfView = 10
	
	-- Lock and slow down mouse
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseDeltaSensitivity = 1
	
	-- Reset zoom angles
	AngleX,TargetAngleX = 0,0
	AngleY,TargetAngleY = 0,0
end

-- Toggle camera zoom/unzoom
local function MouseClick()
	if zoomed then
		-- Unzoom camera
		ResetCamera()
	else
		-- Zoom in camera
		ZoomCamera()
	end
	
	zoomed = not zoomed
end

local cameraRotation = Vector2.new(0,math.rad(-60))
local function MouseMoved(input)
	if zoomed then
		local sensitivity = 0.6		-- anything higher would make looking up and down harder; recommend anything between 0~1
		local smoothness = 0.05		-- recommend anything between 0~1

		local delta = Vector2.new(input.Delta.x/sensitivity,input.Delta.y/sensitivity) * smoothness

		local X = TargetAngleX - delta.y
		local Y = TargetAngleY - delta.x
		TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
		TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
		
		AngleX = AngleX + (TargetAngleX - AngleX) *0.35
		AngleY = AngleY + (TargetAngleY - AngleY) *0.15
		
		camera.CFrame = CFrame.new(head.Position, target)
		* CFrame.Angles(0,math.rad(AngleY),0)
		* CFrame.Angles(math.rad(AngleX),0,0)
	end
end

local function InputBegan(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		MouseClick()
	end
end

local function InputChanged(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		MouseMoved(input)
	end
end

if UserInputService.MouseEnabled then
	UserInputService.InputBegan:Connect(InputBegan)
	UserInputService.InputChanged:Connect(InputChanged)
end