Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Hotline Miami style gun?

I want to create a gun that shoots in the style of Hotline Miami. By this, I mean a gun similar to the one in this video.

My script right now draws a ray from the gun's handle to the mouse's location. My problem is that I don't want it to follow the mouse, but continue straight in the direction the player shoots. Also, the ray shouldn't be able to move on the y axis, because then the bullet's just hit the ground.

Code

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
 
tool.Equipped:connect(function(mouse)
	print("Tool equipped!")
 
	mouse.Button1Down:connect(function()
		print("Mouse pressed!")
		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
 
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Ghost grey")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 
		local distance = (tool.Handle.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.2, 0.2, distance)
		beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
 
		game:GetService("Debris"):AddItem(beam, 0.1)
 
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
 
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
 
			if humanoid then
				humanoid:TakeDamage(30)
			end
		end
	end)
end)

Thanks, -Mex

2 comments

0

xxxdddddddddddd

0

btw u know thats parctically from the wiki right

1 answer

✓ Accepted answer

uhh since im lazy to add an actual tool(im sorry!) ive made this LocalScript that u should put in the StarterPack:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local root

plr.CharacterAdded:Connect(function(char)
	wait(.1)
	root = char:FindFirstChild("HumanoidRootPart")
end)

repeat wait() until root ~= nil

mouse.Button1Down:Connect(function()
	local XZpos = Vector3.new(mouse.Hit.X, root.Position.Y, mouse.Hit.Z)
	
	local ray = Ray.new(root.Position, (XZpos - root.Position).Unit * 300)
	local part, pos = workspace:FindPartOnRay(ray, root.Parent, true, false)
	
	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new("Ghost grey")
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = .25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	
	local dist = (root.Position - pos).Magnitude
	beam.Size = Vector3.new(.2,.2,dist)
	beam.CFrame = CFrame.new(root.Position, pos) * CFrame.new(0,0,-dist / 2)
	
	game:GetService("Debris"):AddItem(beam, .1)
	
	if part then
		local hum = part.Parent:FindFirstChild("Humanoid")
		
		if not hum then
			hum = part.Parent:FindFirstChild("Humanoid")
		end
		
		if hum then
			hum:TakeDamage(30)
		end
	end
end)

im sorry to have not added any description(as well) but this basically substitutes the mouse.Hit.Y with the root.Position.Y, making the ray not affected by the mouse.Hit.Y but instead the root.Position.Y

3 comments

0

i still dont know anything about rays but this seems helpful

0

Thanks man, this is just what I needed!

0

xd

This discussion is preserved as it was. New answers, comments, and votes are closed.