Hip Fire on a gun with ScreenPointToRay

So I have been reading on the devforms for anything about hip fire related things. I also have been thinking about how to make it. I know you need to lock the mouse in the middle so it will be with the camera, and with a Gui. But how would I make the hip fire since the bullet would have to go anywhere inside the Gui? Would ScreenPointToRay, ViewportToRay, or WorldToScreenPoint work?

you dont have to lock the mouse in the middle

you can just make a normal 3rd person gun that shout a lot of bullets

Ya but I want them to not be as accurate when you are not aiming in such as the sniper from the weapon’s kit.

Alright so I figured out that I should use ScreenPointToRay but how would I get the inside of the crosshair?

Alright so I got this to work if anyone wants to know how I got it to work here is the code:
BTW this is in a gui, the cross frame is just a frame to get the inside of the reticle.

local player = game.Players.LocalPlayer
local UserInput = game:GetService("UserInputService")
local CrossFrame = script.Parent.CrossFrame
local camera = game.Workspace.CurrentCamera
local Debris = game:GetService("Debris")
local Character = player.Character or player.CharacterAdded:Wait()
local rootpart = Character:WaitForChild("HumanoidRootPart")

local LaserDamage = 20

UserInput.InputBegan:Connect(function(input, chat)
if not chat then
	if input.KeyCode == Enum.KeyCode.T then
    local length = 100
	 --This would randomly put it in the camera lookVector and not inside the crosshair local middleof_screen = (camera.ViewportSize - (CrossFrame.AbsolutePosition* 2))/2
	  local middleof_screen = (CrossFrame.AbsolutePosition* 2)/2	
	 local crosshair_fireX = math.random(CrossFrame.AbsoluteSize.X) --+ 25)
	  local crosshair_fireY = math.random(CrossFrame.AbsoluteSize.Y) --+ 25 )
	   local unitray = camera:ScreenPointToRay(middleof_screen.X + crosshair_fireX, middleof_screen.Y + crosshair_fireY)
		  local ray = Ray.new(unitray.Origin, unitray.Direction * length)
			local HitPart, HitPosition = game.Workspace:FindPartOnRay(ray, Character, false, true)

			local LaserBeam = Instance.new("Part", game.Workspace)
			LaserBeam.BrickColor = BrickColor.new("Really red")
			LaserBeam.FormFactor = "Custom"
			LaserBeam.Shape = "Block"
			LaserBeam.Material = "Neon"
			LaserBeam.Transparency = 0.3
			LaserBeam.Anchored = true
			LaserBeam.CanCollide = false
			
			local LaserDistance = (rootpart.CFrame.p - HitPosition).Magnitude
			LaserBeam.Size = Vector3.new(.25, .25, LaserDistance)
			LaserBeam.CFrame = CFrame.new(rootpart.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)
			
			print(unitray)
				Debris:AddItem(LaserBeam, 0.4)
				
				if HitPart then
					local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")

					if not HitHumanoid then
						HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")
					end

					if HitHumanoid then
						HitHumanoid:TakeDamage(LaserDamage)
						
				end
			end
		end
	end
end)
2 Likes