What do you want to achieve? Smooth camera for mobile
What is the issue? When player is on mobile and moves forward with a joystick, it records a mouse delta which moves the camera up
What solutions have you tried so far? None, tried looking at devforum and tried asking on discord
--Current code
local RS = game:GetService('RunService')
local UIS = game:GetService('UserInputService')
local Player = game:GetService('Players').LocalPlayer
local Camera = workspace.CurrentCamera
local Turn = 0
local VelocityX = 0
local VelocityY = 0
local Sensitivity = 0.85
local Lerp = function(a,b,t)
return a + (b - a) * t
end
RS:BindToRenderStep("SmoothCamera", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
if UIS.KeyboardEnabled and UIS.MouseEnabled and not UIS.TouchEnabled then
--pc
local MouseDelta = UIS:GetMouseDelta()
Turn = Lerp(Turn, math.clamp(MouseDelta.X, -4, 4), (4 * deltaTime))
VelocityX = Lerp(VelocityX, math.clamp(MouseDelta.X, -0.5, 0.5), (10 * deltaTime))
VelocityY = Lerp(VelocityY, math.clamp(MouseDelta.Y, -0.5, 0.5), (10 * deltaTime))
Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(-VelocityY) * Sensitivity, math.rad(-VelocityX) * Sensitivity, math.rad(Turn))
elseif not UIS.KeyboardEnabled and UIS.TouchEnabled and not UIS.MouseEnabled then
--mobile
--Here put mobile mouse delta (without joystick movement)
end
end)
i asume you want the mouse to act on the joystick’s movement rather than the mouse’s position, since ofcourse, the player on mobile has no mouse, and would move to where the player puts their finger all the time.
to achieve this, you can use:
local PlayerModule = require(Player.PlayerScripts.PlayerModule)
local moveVector = PlayerModule:GetControls():GetMoveVector()
The moveVector in this scenario indicates the vector where the players joystick is moving towards.
Here’s an example of how i used this moveVector in to manipulate a camera.
if moveVector.magnitude > 0 then
local horizontalMoveVector = Vector3.new(moveVector.x, 0, moveVector.z)
if horizontalMoveVector.magnitude > 0 then
-- Adjusted movement with decreased interpolation factor
local newLookDirection = camera.CFrame.LookVector:Lerp(horizontalMoveVector.unit, 0.05)
camera.CFrame = CFrame.new(camera.CFrame.Position, camera.CFrame.Position + newLookDirection)
end
end
You can utilize this moveVector the same way you did with pc, ofcourse keeping in mind to put an if statement on its magnitude.
Thanks, but i wanted the opposite. I want the delta of player’s camera movement, if he’s touching the joystick then the camera is responsive and mouse delta is still giving out info. If i’d check for joystick movement then the camera wouldn’t work when both joystick and camera is moving. Is there any way to get only movement from the right side of the player’s screen?
You could achieve this by checking the input from both the joystick and the mouse separately, and then deciding which input to use for camera movement based on whether the joystick is active.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local joystickActive = false
-- Function to handle mouse input
local function handleMouseInput()
local mouseDelta = UserInputService:GetMouseDelta()
-- Process mouse delta and apply camera movement here
-- For example:
-- camera.CFrame = camera.CFrame * CFrame.new(0, 0, -mouseDelta.X)
end
-- Function to handle joystick input
local function handleJoystickInput()
local joystickInput = UserInputService:GetGamepadState(Enum.UserInputType.Gamepad1)
if joystickInput and joystickInput.Position.magnitude > 0 then
joystickActive = true
else
joystickActive = false
end
end
-- Function to update camera movement
local function updateCamera()
if not joystickActive then
handleMouseInput()
end
end
-- Connect input handlers
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
updateCamera()
end
end)
UserInputService.InputBegan:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
handleJoystickInput()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
joystickActive = false
end
end)
to make the code sepparate the 2 inputs players on mobile have with multiple fingers, you could listen for input on the right half of the screen, here is a modified version of the code earlier.
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local joystickActive = false
local lastTouchPosition = nil
-- Function to handle mouse input
local function handleMouseInput()
local mouseDelta = UserInputService:GetMouseDelta()
-- Process mouse delta and apply camera movement here
-- For example:
-- camera.CFrame = camera.CFrame * CFrame.new(0, 0, -mouseDelta.X)
end
-- Function to handle joystick input
local function handleJoystickInput()
local joystickInput = UserInputService:GetGamepadState(Enum.UserInputType.Gamepad1)
if joystickInput and joystickInput.Position.magnitude > 0 then
joystickActive = true
else
joystickActive = false
end
end
-- Function to handle touch input
local function handleTouchInput(touchPositions)
if lastTouchPosition then
-- Find the position of the touch on the right side of the screen
local rightTouchPosition
for _, position in pairs(touchPositions) do
if position.X > (workspace.CurrentCamera.ViewportSize.X / 2) then
rightTouchPosition = position
break
end
end
if rightTouchPosition then
-- Calculate delta position of touch on the right side of the screen
local touchDelta = rightTouchPosition - lastTouchPosition
-- Process touch delta and apply camera movement here
-- For example:
-- camera.CFrame = camera.CFrame * CFrame.new(0, 0, -touchDelta.X)
end
end
lastTouchPosition = touchPositions[1]
end
-- Function to update camera movement
local function updateCamera()
handleJoystickInput()
handleMouseInput() -- Allow camera to respond to mouse input even when joystick is active
end
-- Connect input handlers
UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
updateCamera()
elseif input.UserInputType == Enum.UserInputType.Touch then
handleTouchInput(input.Position)
end
end)
UserInputService.InputBegan:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
handleJoystickInput()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
joystickActive = false
elseif input.UserInputType == Enum.UserInputType.Touch then
lastTouchPosition = nil -- Reset lastTouchPosition when touch ends
end
end)
If a touch is detected on the right side of the screen, the camera movement should be updated using the calculated touch delta rightTouchPosition.
ohh i see what you send, i was talking about the joystick as in ui in phone on roblox, but it still didn’t work even when just moving the camera, i think i will just stick with smooth camera only on pc’s and normal one on phones