I need to convert this line of code to work on mobile.
local mouseDirection = (Mouse.Hit.Position - FirePointObject.WorldPosition).Unit
It works as is, but it fires the weapon at the point touched on the screen.
That would be great, but if multiple places are touched (with two thumbs) then it will switch back and forth between the two places.
This is a real problem when the player is walking since they are pressing the screen in two places at the same time. (GameProcessed does not seem to eliminate the joystick).
Does anyone know how to convert it use Camera.CFrame.Position?
I got it to work with Camera.CFrame.Position (sort of), but it always shoots at the bottom of the screen,
So you’re trying to raycast from the screen centre?
step 1:
Get a position for the centre of the screen.
local pos = UDim2.fromScale(0, 0)
step 2:
Turn it into a ray
local ray = workspace.CurrentCamera:ScreenPointToRay(pos.X.Scale, pos.Y.Scale)
step 3:
Raycast
local params = RaycastParams.new()
--do stuff with your RaycastParams
local hit = workspace:Raycast(ray.Origin, ray.Direction * 300, params)
if hit then
print(hit)
end
yeah, no, there’s no getting around this. (thanks multibillion dollar company!) i recommend a crosshair when moving, and maybe the ability to shoot freely if there’s one input
I get the same result I got with all of the other ray cast suggestion I tried (from reading other post, the docs and watching videos).
I added your suggestion, but it prints “No Touch_Hit”.
local function OnEquipped()
ConnectRunService = game:GetService("RunService").Stepped:Connect(function ()
if IsMouseDown then
local Touch_RayPos = UDim2.fromScale(0,0)
local Touch_Ray = workspace.CurrentCamera:ScreenPointToRay(Touch_RayPos.X.Scale, Touch_RayPos.Y.Scale)
local Touch_Params = RaycastParams.new()
local Touch_Hit = workspace:Raycast(Touch_Ray.Origin, Touch_Ray.Direction * 300, Touch_Params)
if Touch_Hit then
--local mouseDirection = (Mouse.Hit.Position - FirePointObject.WorldPosition).Unit
local mouseDirection = (Touch_Hit - FirePointObject.WorldPosition).Unit
for i = 1, BULLETS_PER_SHOT do
local directionalCF = CFrame.new(Vector3.new(), mouseDirection)
local direction = (directionalCF * CFrame.fromOrientation(0, 0, Random.new():NextNumber(0, math.pi * 2)) * CFrame.fromOrientation(math.rad(Random.new():NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local simBullet = Caster:Fire(FirePointObject.WorldPosition, direction, modifiedBulletSpeed, CastBehavior)
end
else
print("No Touch_Hit")
end
end
end)
end
Item.Equipped:Connect(OnEquipped)
UserInputService.InputBegan:Connect(function (input, gameHandledEvent)
if gameHandledEvent or not ExpectingInput then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then IsMouseDown = true end
end)
UserInputService.InputEnded:Connect(function (input, gameHandledEvent)
if gameHandledEvent or not ExpectingInput then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then IsMouseDown = false end
end)
You are trying to emit a ray from the center of the screen right? Why not just do Enum.MouseBehaviour.LockCenter? I think it would work for mobile players, haven’t tested yet.