Part Follows PC Mouse – Suggestions to Duplicate on Mobile Please

In my game an arrow part (Shooter) follows the mouse button while the mouse target is over a certain part (Set.Front). When you click the mouse, a ball at the arrow part fires in the direction the arrow part is pointed. Think Puzzle Bobble if you’re familiar. This all works perfectly on PC…

…but I’m not real familiar with the workings of mobile. My thought is I would like the arrow part only to move once the user is touching the screen and only after touching the Set.Front part, then to follow the pointer while dragging, then to fire the ball when releasing. I’m guessing this is some combination of TouchStarted, TouchMoved and TouchEnded functions, but my research has me wrapped around the axle a bit. Appreciate a nudge in the right direction.

Here is my current script – again, all PC function with the mouse is perfect. The Touch stuff is a mess. With this current iteration while on mobile the shooter does not move, though the ball does fire when the touch is released. I also have to work out the issue of the camera on mobile rotating while dragging, which should be stopped, at least temporarily, while dragging to move the shooter arrow.

Not working:

local function ShooterMovement(Set)
	local Shooter = Set.Shooter
	if UserInputService.TouchEnabled then
		UserInputService.TouchMoved:Connect (function(Input)
			--if Input.position == Set.Front then
			local pos = Vector3.new(Shooter.Position.X, Input.Position.Y, Input.Position.Z)
			Shooter.CFrame = CFrame.new(Shooter.Position, pos)	
			--end
		end)
	else

this part works fine:

		Mouse.Move:Connect(function()
			if Mouse.target == Set.Front then
				local pos = Vector3.new(Shooter.Position.X, Mouse.Hit.Position.Y, Mouse.Hit.Position.Z)
				Shooter.CFrame = CFrame.new(Shooter.Position, pos)
				--game.ReplicatedStorage.RemoteEventShooterUpdate:FireServer(Set,Shooter.CFrame) --- UPDATE SHOOTER POS ON SERVER - TEMPORARILY DISABLED TO REDUCE LAG
			end
		end)
	end
end

And for shooting, actually this first part does work to fire the ball, although the shooter is fixed, and again the mouse portion further down functions fine.

-------------------- SHOOT BALL -------------------------
local function Shoot(Set)
	local MouseDB = false
	if UserInputService.TouchEnabled then
		UserInputService.TouchEnded:Connect(function()
		    Set.ActiveBall:ApplyImpulse(Set.Shooter.CFrame.LookVector * 80) --<<<<<<<<<< SHOOT THE BALL
			Player.character.Head.Shoot:Play()
			BallInPlay = true
		end)
	else
		Mouse.Button1Down:Connect(function()
			Set.ActiveBall:ApplyImpulse(Set.Shooter.CFrame.LookVector * 80) --<<<<<<<<<< SHOOT THE BALL
			Player.character.Head.Shoot:Play()
			BallInPlay = true
		end)
	end
end

You can use for PC and mobile mouse.Button1Up/Button1Down. Here is example if you hold (mouse,display) more then 1 second then you change direction gun, if not then gun shooted.

--Service
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--Players
local player = Players.LocalPlayer
local mouse = player:GetMouse()

--Value
local timeDown = 0
local hold = false

function ShootBall()
	print("Shoot")
end

function ShooterMovement()
	print("Change direction")
end

RunService.RenderStepped:Connect(function()
	if hold == true and timeDown == 10 and mouse.Target == "YourPart" then -- run if you hold mouse down, count down 10*0.1sec (1 second) and mouse target is part..
		ShooterMovement()
	end
end)

mouse.Button1Up:Connect(function() -- mouse up = change hold to false
	hold = false
end)

mouse.Button1Down:Connect(function() -- mouse hold < 1 sec = SHOT, mouse hold > 1 sec = CHANGE DIRECTION
	hold = true
	timeDown = 0
	repeat -- repeat until mouse doesnt change or it counts to 10. (10*0.1sec) = 1 second
		task.wait(0.1)  
		timeDown += 1
	until (hold == false or timeDown == 10)
	if timeDown < 10 then -- if it fails to count to 10 then fire
		ShootBall()
	end
end)
1 Like

EDIT - Actually the below works (turning camera type to ‘scriptable’). I just had a simple error.

Thank you so much for the response; very helpful. Using mouse.button function, even while on mobile will work for me. I’m now down to a very small issue to solve; not sure if I should make a separate post, but I’ll start here:

How can I temporarily disable the mobile feature of turning the camera while dragging? I’ve tried making the camera Scriptable temporarily, but the dragging rotation persists.

UserInputService.TouchMoved:Connect (function(Input)
			Mouse.Move:Connect(function()
				if Mouse.target == Set.Front then
					game.Workspace.Camera.CameraType = "Scriptable" -- WHAT ELSE HERE WOULD MAKE IT SO THAT MOBILE DRAGGING DID NOT ROTATE THE CAMERA WHILE THE USER HAS TOUCH/HELD OVER THE 'Set.Front' PART??
					local pos = Vector3.new(Shooter.Position.X, Mouse.Hit.Position.Y, Mouse.Hit.Position.Z)
					Shooter.CFrame = CFrame.new(Shooter.Position, pos)
					--game.ReplicatedStorage.RemoteEventShooterUpdate:FireServer(Set,Shooter.CFrame) --- UPDATE SHOOTER POS ON SERVER - TEMPORARILY DISABLED TO REDUCE LAG
				end
			end)
		end)

Cheers and thanks!!

you must use “CurrentCamera” instead of “Camera”.

local currentCamera = workspace.CurrentCamera
local Part = "YourPart"

--Set camera to part
currentCamera.CameraType = Enum.CameraType.Scriptable
currentCamera.CameraSubject = Part -- the camera will be set to part
currentCamera.CFrame = Part.CFrame -- locks the angle of view

--Set camera back
currentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
currentCamera.CameraType = Enum.CameraType.Custom
1 Like