Center Of Screen instead of Mouse.Hit.Position

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,

1 Like
1 Like

I spent 3 hours reading through that and countless forum posts.

None of the solutions worked.

1 Like

Well that’s weird. You are tryng to emit a ray from the center of the screen right? Just so we are on the same page?

2 Likes

I tried every solution posted about Ray Casting and none of them worked.

There are 3 lines of code that work together:

local mouseDirection = (Mouse.Hit.Position - FirePointObject.WorldPosition).Unit
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

1 Like

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

I hope this helps.

real big help aren’t you…

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

The ray cast does not work for some reason.

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 also aren’t of any help. It’s best if you simply leave the topic if you dont have anything really valuable to say.

As for OP @mc7oof :

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.

I fail to understand the issue.