Need help on setting orientation of cylinder raycast

Hello, I have a test raycast gun that I would like to have a cylinder shaped laser, but when I try to set the orientation of the cylinder, it doesn’t work. The gun still functions, I just want to change the shape of the raycast. I don’t know why the orientation isn’t working! (Yes, this is a free model.)

Here is my entire script. The raycast bullet is called “p” in the script.

local settings = {
	["tooltip"] = "Produced by HTAA"; -- Tooltip that will pop up when the mouse is hovered over the item
	["name"] = ".56 MG"; -- Name of the gun
	["dtime"] = 5; -- Amount of time before a bullet disappears
	["cooldown"] = .1; -- Time between shots
	["rldtime"] = 1; -- Reload time
	["maxammo"] = 32; -- Amount of ammo in one magazine
	["damage"] = 8; -- Amount of damage one bullet does
	["automatic"] = true; -- If set to true, the gun will continously fire if the left mouse button is held down
	["range"] = 1500; -- Maximum distance a bullet can travel in studs
	["reloadwhenout"] = true; -- Automatically reloads the weapon if your ammo is at 0
}



db = false
local rld = false
local ammo = settings["maxammo"]
local plr = game.Players.LocalPlayer;
repeat wait() until plr.Character;
local char = plr.Character;
local mouse = plr:GetMouse()
local tool, handle = script.Parent, script.Parent:FindFirstChild("Handle");
local r = game:GetService("RunService")
local hold = false
local enabled = false

tool.ToolTip = settings["tooltip"]
tool.Name = settings["name"].." ".."["..ammo.."]"

local function reload()
			if ammo <= 31 then
			if rld == false then rld = true
			if db == false then db = true
			tool.Reload:Play()
			tool.Name = "REL."
				wait(settings["rldtime"]/3)
				tool.Name = "REL.."
				wait(settings["rldtime"]/3)
				tool.Name = "REL..."
				wait(settings["rldtime"]/3)
			ammo = settings["maxammo"]
			tool.Name = settings["name"].." ".."["..ammo.."]"
			end
			end
		rld = false
	db = false
	end
end


local function FireRay()
	if enabled == false then return end
	if ammo > 0 then
	if db == false then db = true
tool.Name = settings["name"].." ".."["..ammo.."]"
tool.Shot:Play()
		ammo = ammo - 1
	local ray = Ray.new(handle.Position,(mouse.Hit.p-handle.Position).unit*settings["range"])
		local hit,pos = workspace:FindPartOnRay(ray,char)
		tool.Name = settings["name"].." ".."["..ammo.."]"
		if hit ~= nil then
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid:TakeDamage(settings["damage"])
			end
		end
			local p = Instance.new("Part",workspace)
			p.Anchored = true
			p.CanCollide = false
			p.Name = "Bullet"
			p.BrickColor = BrickColor.new("New Yeller")
			p.FormFactor = "Custom"
			p.TopSurface = "Smooth"
			p.BottomSurface = "Smooth"
			p.Shape=Enum.PartType.Cylinder
			p.Orientation=Vector3.new(0,0,90) --Does not work?
			
						local s = (pos-handle.Position).magnitude
					p.Size = Vector3.new(.2,.2,s)
			p.CFrame = CFrame.new(pos,handle.Position)*CFrame.new(0,0,-s/2)
	coroutine.resume(coroutine.create(function()
		wait(settings["dtime"])
		p:Destroy()
	end))
	end
	end
	wait(settings["cooldown"])
	db = false
	if rld == true then db = true
	end
end

tool.Equipped:connect(function(mouse2)
	if enabled == false then enabled = true
mouse2.Icon = "rbxasset://textures\\GunCursor.png"
if settings["automatic"] == true then
	mouse.Button1Down:connect(function()
		hold = true
			while hold do
				r.RenderStepped:wait()
				FireRay()
				if ammo <= 0 and settings["reloadwhenout"] == true then
					reload()
				end
			end
		end)
			mouse.Button1Up:connect(function()
					hold = false
		end)
else
	mouse.Button1Down:connect(function()
		FireRay()
		if ammo <= 0 and settings["reloadwhenout"] == true then
			reload()
		end
	end)
end
	end
	tool.Unequipped:connect(function()
		if enabled == true then enabled = false
		else
			return
		end
	end)
end)

mouse.KeyDown:connect(function(key)
	if enabled == false then return end
	if key:lower() == "r" then
	reload()
	end
end)
1 Like

If you wanted to make a “regular” raycast, i wouldn’t use this depreciated method. I’d use workspace:Raycast but that’s besides the main point.

Try working with SphereCast

I’m not really knowledgeable on raycasts, so I don’t really care if this is a deprecated script. It still works.
I just want to know if I can just change the orientation of the cylinder.

You need to multiply the CFrame by a CFrame.Angles vector. And I know you don’t care that this script is deprecated, but don’t be surprised if lag occurs and if it doesn’t perform optimally.

Code:

local settings = {
	["tooltip"] = "Produced by HTAA"; -- Tooltip that will pop up when the mouse is hovered over the item
	["name"] = ".56 MG"; -- Name of the gun
	["dtime"] = 5; -- Amount of time before a bullet disappears
	["cooldown"] = .1; -- Time between shots
	["rldtime"] = 1; -- Reload time
	["maxammo"] = 32; -- Amount of ammo in one magazine
	["damage"] = 8; -- Amount of damage one bullet does
	["automatic"] = true; -- If set to true, the gun will continously fire if the left mouse button is held down
	["range"] = 1500; -- Maximum distance a bullet can travel in studs
	["reloadwhenout"] = true; -- Automatically reloads the weapon if your ammo is at 0
}



db = false
local rld = false
local ammo = settings["maxammo"]
local plr = game.Players.LocalPlayer;
repeat wait() until plr.Character;
local char = plr.Character;
local mouse = plr:GetMouse()
local tool, handle = script.Parent, script.Parent:FindFirstChild("Handle");
local r = game:GetService("RunService")
local hold = false
local enabled = false

tool.ToolTip = settings["tooltip"]
tool.Name = settings["name"].." ".."["..ammo.."]"

local function reload()
	if ammo <= 31 then
		if rld == false then rld = true
			if db == false then db = true
				tool.Reload:Play()
				tool.Name = "REL."
				wait(settings["rldtime"]/3)
				tool.Name = "REL.."
				wait(settings["rldtime"]/3)
				tool.Name = "REL..."
				wait(settings["rldtime"]/3)
				ammo = settings["maxammo"]
				tool.Name = settings["name"].." ".."["..ammo.."]"
			end
		end
		rld = false
		db = false
	end
end


local function FireRay()
	if enabled == false then return end
	if ammo > 0 then
		if db == false then db = true
			tool.Name = settings["name"].." ".."["..ammo.."]"
			tool.Shot:Play()
			ammo = ammo - 1
			local ray = Ray.new(handle.Position,(mouse.Hit.p-handle.Position).unit*settings["range"])
			local hit,pos = workspace:FindPartOnRay(ray,char)
			tool.Name = settings["name"].." ".."["..ammo.."]"
			if hit ~= nil then
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid:TakeDamage(settings["damage"])
				end
			end
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Name = "Bullet"
			p.BrickColor = BrickColor.new("New Yeller")
			p.FormFactor = "Custom"
			p.TopSurface = "Smooth"
			p.BottomSurface = "Smooth"
			p.Shape=Enum.PartType.Cylinder

			local s = (pos-handle.Position).magnitude
			p.Size = Vector3.new(.2,.2,s)
			p.CFrame = CFrame.new(pos,handle.Position)*CFrame.new(0,0,-s/2) * CFrame.Angles(0, 0, math.rad(180)) --//Change orientation
			p.Parent = workspace

			coroutine.resume(coroutine.create(function()
				wait(settings["dtime"])
				p:Destroy()
			end))
		end
	end
	wait(settings["cooldown"])
	db = false
	if rld == true then db = true
	end
end

tool.Equipped:connect(function(mouse2)
	if enabled == false then enabled = true
		mouse2.Icon = "rbxasset://textures\\GunCursor.png"
		if settings["automatic"] == true then
			mouse.Button1Down:connect(function()
				hold = true
				while hold do
					r.RenderStepped:wait()
					FireRay()
					if ammo <= 0 and settings["reloadwhenout"] == true then
						reload()
					end
				end
			end)
			mouse.Button1Up:connect(function()
				hold = false
			end)
		else
			mouse.Button1Down:connect(function()
				FireRay()
				if ammo <= 0 and settings["reloadwhenout"] == true then
					reload()
				end
			end)
		end
	end
	tool.Unequipped:connect(function()
		if enabled == true then enabled = false
		else
			return
		end
	end)
end)

mouse.KeyDown:connect(function(key)
	if enabled == false then return end
	if key:lower() == "r" then
		reload()
	end
end)

Did work, but I assumed that changing the orientation would allow the cylinder to scale properly down it’s length rather than its width, which it did not. So, this is an unrelated problem it seems. Sorry for the trouble.

Try changing the .Size then.

Code:

local settings = {
	["tooltip"] = "Produced by HTAA"; -- Tooltip that will pop up when the mouse is hovered over the item
	["name"] = ".56 MG"; -- Name of the gun
	["dtime"] = 5; -- Amount of time before a bullet disappears
	["cooldown"] = .1; -- Time between shots
	["rldtime"] = 1; -- Reload time
	["maxammo"] = 32; -- Amount of ammo in one magazine
	["damage"] = 8; -- Amount of damage one bullet does
	["automatic"] = true; -- If set to true, the gun will continously fire if the left mouse button is held down
	["range"] = 1500; -- Maximum distance a bullet can travel in studs
	["reloadwhenout"] = true; -- Automatically reloads the weapon if your ammo is at 0
}

local db = false
local rld = false
local ammo = settings["maxammo"]
local plr = game.Players.LocalPlayer;
local char = plr.Character or plr.CharacterAdded:Wait();
local mouse = plr:GetMouse()
local tool, handle = script.Parent, script.Parent:FindFirstChild("Handle");
local r = game:GetService("RunService")
local hold = false
local enabled = false

tool.ToolTip = settings["tooltip"]
tool.Name = settings["name"].." ".."["..ammo.."]"

local function reload()
	if ammo <= 31 then
		if rld == false then rld = true
			if db == false then db = true
				tool.Reload:Play()
				tool.Name = "REL."
				task.wait(settings["rldtime"]/3)
				tool.Name = "REL.."
				task.wait(settings["rldtime"]/3)
				tool.Name = "REL..."
				task.wait(settings["rldtime"]/3)
				ammo = settings["maxammo"]
				tool.Name = settings["name"].." ".."["..ammo.."]"
			end
		end
		rld = false
		db = false
	end
end


local function FireRay()
	if enabled == false then return end
	if ammo > 0 then
		if db == false then db = true
			tool.Name = settings["name"].." ".."["..ammo.."]"
			tool.Shot:Play()
			ammo = ammo - 1
			local ray = Ray.new(handle.Position,(mouse.Hit.p-handle.Position).unit*settings["range"])
			local hit,pos = workspace:FindPartOnRay(ray,char)
			tool.Name = settings["name"].." ".."["..ammo.."]"
			if hit ~= nil then
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid:TakeDamage(settings["damage"])
				end
			end
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.Name = "Bullet"
			p.BrickColor = BrickColor.new("New Yeller")
			p.FormFactor = "Custom"
			p.TopSurface = "Smooth"
			p.BottomSurface = "Smooth"
			p.Shape=Enum.PartType.Cylinder

			local s = (pos-handle.Position).magnitude
			p.Size = Vector3.new(s, .2, .2)
			p.CFrame = CFrame.new(pos,handle.Position)*CFrame.new(0,0,-s/2)
			p.Parent = workspace
			
			task.delay(settings.dtime, function()
				p:Destroy()
			end)
		end
	end
	
	task.wait(settings["cooldown"])
	db = false
	if rld == true then 
		db = true
	end
end

tool.Equipped:connect(function(mouse2)
	if enabled == false then enabled = true
		mouse2.Icon = "rbxasset://textures\\GunCursor.png"
		if settings["automatic"] == true then
			mouse.Button1Down:connect(function()
				hold = true
				while hold do
					r.RenderStepped:wait()
					FireRay()
					if ammo <= 0 and settings["reloadwhenout"] == true then
						reload()
					end
				end
			end)
			mouse.Button1Up:connect(function()
				hold = false
			end)
		else
			mouse.Button1Down:connect(function()
				FireRay()
				if ammo <= 0 and settings["reloadwhenout"] == true then
					reload()
				end
			end)
		end
	end
	tool.Unequipped:connect(function()
		if enabled == true then enabled = false
		else
			return
		end
	end)
end)

mouse.KeyDown:connect(function(key)
	if enabled == false then return end
	if key:lower() == "r" then
		reload()
	end
end)

Oh, this did actually work for me. I’m an idiot. Thank you.

1 Like

Don’t worry, it’s hard to debug old code especially if you’re a beginner. If you have any more questions, feel free to ask.

1 Like

Do you know any good tutorials on current raycast guns that include visualization of the raycast at all?
I’m a real novice at scripting and I am not spending a week just figuring that out on my own. All the tutorials I see for a “raycast gun” either use the deprecated Ray.new or they use the updated raycast but they don’t visualize the bullet with a part.

This is an extremely good tutorial that I used when I was a beginner to make my first gun:

It uses FastCast, which is a wrapper for raycasts that allows for very realistic projectile trajectories.

I mean like Prison Life guns for example. A straight beam part with no physics, not an actual projectile. I didn’t elaborate enough.

Roblox actually has a tutorial for that: Hit Detection with Lasers | Documentation - Roblox Creator Hub

Thank you! This is exactly what I was referring to. You’re the best.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.