What do you want to achieve? Keep it simple and clear!
I am trying to support mobile input for a gun script. The objective is for the bullet to hit where the person tapped. The game Weapon Kit is an example.
It supports cursor ‘ button to fire and tap to click
What is the issue? Include screenshots / videos if possible!
I tried to use some fancy maths script to translate click to world position. It does fire on tap but I noticed that the further the distance from camera center, the more wide a strange offset will happen. . Ie if the target is near both in distance and angle from Center, hit is achieved. If the target is far and/ or at an angle away from Center, the bullet will fly wide above / side.
I just need a simple click here and bullet goes here solution
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Try to search around and around but nothing I can think of that helps.
It’s possible to make using a combination of user inputservices or context action service to detect input, then using screen point to ray to cast a ray straight forwards into the camera:
This is the script i found and use: Stravant’s ScreenSpace Module script
It is from a gun which has the below for local script that connects to the module.
local function mobileFrame(touch, processed)
if not processed then
local test = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1)
local nearPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1))
nearPos = game.Workspace.CurrentCamera.CoordinateFrame.p - nearPos
local farPos = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y,50)
farPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(farPos) * -1
if farPos.magnitude > 900 then
farPos = farPos.unit * 900
end
local ray = Ray.new(nearPos, farPos)
local part, pos = game.Workspace:FindPartOnRay(ray, player.Character)
if pos then
frame(pos)
end
end
end
So what i did was i used this section and plug it into the fast cast example gun to provide it the mobile support. FastCast: Redux Example Gun - Roblox
Now i cant aim properly beyond a small range of distance / angle
the strange thing is that on a pc with mouse, the aim / fire is laser precision. i am not sure why is it so difficult on roblox to just take a mobile tap as a mouse click on that point . . ( maybe i do not know how but i cant find any thing about it )
i found the solution for my problem. skip the fancy whatever.
JUST USE
This will work and it is a clean solution for you to fireserver to then process the ray. I hope it doesnt get deprecated and then i have to go figured out the userinputservice which doesnt have the internal ray of mouse.hit.
Sorry, but it’s already deprecated by user input services as seen in the mouse object in the dev reference API.
Good job researching though. Anyways if you want to get where the mouse is hit using UserInputServices you can do this:
local UserInputService = game:GetService("UserInputService")
--Variables for the screen to ray function
local playerCam = workspace.CurrentCamera
local rayLen = 4000
local function getMousePosOrLookVector()
--Obtains the xy coordinates on screen
local mouseLoc = UserInputService:GetMouseLocation()
--checks if nothin is wrong with mouse Location
if mouseLoc~=nil then
local camRay = playerCam:ViewportPointToRay(mouseLoc.X, mouseLoc.Y, 0)
local camToWorldRay = Ray.new(camRay.Origin, camRay.Direction * rayLen)
local origin = camToWorldRay.Origin
local dir = camToWorldRay.Direction
local result = workspace:Raycast(origin,dir,normRay)
if result~= nil then
--print(result) --prints result for debugging purposes-
return result.Position,true
else
return camRay.Direction,false
end
end
end
This may not be the best way as it’s what I did while learning how to code but it works for now. Moreover, it even works for mobile for some reason which I tested out on studio, however, I have some offsetting issues probably due to CFrame stuff Idk I would rework my system.
Edit: It’s probably because of this property within the mouse object which they used for user input services.
While a mouse may not be available on all platforms, Mouse will still function on mobile (touch) and console (gamepad), which don’t typically have mice or pointer hardware. For explicit cross-platform behaviors, use UserInputService and ContextActionService .