Mobile Aiming System

Hello!

Can I make it so a gun will only shoot to a spot on your view.

This is what I mean,

So it would shoot at the white dot there.

Is that something that’s even possible?
If so, please let me know how to do so.

Thanks!

1 Like

Similar to the Neo Knives gun. : D

Of course it’s possible, Here are the steps:

  1. Get Mouse Location on the screen through GetMouseLocation() to get the X,Y coordinates
  1. Cast a ray from the camera using screen point to ray to a shoot a ray with a direction starting from the camera screen straight forwards (into the page) towards the white dot.

Edit: NVM for mobile instead of mouse location you’ll need to get the touch position in UserInputService as @royaltoe dmed me like TouchTap() I have no experience in that however so please refer to the API.

2 Likes

Alright, Ill read up on this stuff!
Thanks!

But the point in the post was the gun to shoot to the gui point. So the white dot stays still and the gun shoots to the white dot.

: p

Oh if thats the case then you can just get the white dot’s (X,Y) coordinates on the screen and raycast from there.

How would I do that? I have no clue. xD

Oh, it’s all in the API referencing

Each screen gui (white dot in your example) is positioned on the screen by the XY coordinate system from the top left of the screen. Consequently, you can work with the white dot gui’s screen position.

Alrighty! Ill try to put something together with all of this! Thank you!

Can you apply it to this script?
local Camera = workspace.CurrentCamera

local Player = game:GetService(“Players”).LocalPlayer

local Mouse = Player:GetMouse()

–This will create a cube facing centered at the origin of the [[Ray]] returned and facing in the same direction. The center of the cube will be one stud in front of the camera.

Mouse.Button1Down:Connect(function()

local ray = Camera:ScreenPointToRay(533, 191, 1)

local cube = Instance.new(“Part”, workspace)

cube.Size = Vector3.new(1, 1, 1)

cube.CFrame = CFrame.new(ray.Origin, ray.Origin + ray.Direction)

cube.Anchored = true

end)

Im completely stumped xD

What’s wrong with it in the first place?

edit: seems to work just fine, btw try using three back ticks to format code ```

Its probably the X,Y coordinates since it varies on each screen size since the units is pixels

local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

--This will create a cube facing centered at the origin of the [[Ray]] returned and facing in the same direction. The center of the cube will be one stud in front of the camera.
Mouse.Button1Down:Connect(function()
	local ray = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)
	local cube = Instance.new("Part", workspace)
	cube.Size = Vector3.new(1, 1, 1)
	cube.CFrame = CFrame.new(ray.Origin+ray.Direction, ray.Origin + 5*ray.Direction)
	cube.Anchored = true
end)

1 Like

Oh wait, never mind, just realized this will work!

1 Like