Laser not facing tool when it is a cylinder

I was making a laser gun but I used a block to test it once I finished it but when I change the part to a cylinder it will face to the right/left. I have tried changing the orientation but it stays the same. This is probably easy to fix but I just need help.

-- The code for the laser
local Tool = script.Parent
local Handle = Tool.Handle
local Tip = Handle.Tip
local Event = Tool:WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(player, mouse)
	local Character = player.Character
	local Laser = Ray.new(Tip.CFrame.p, (mouse - Tip.CFrame.p).unit * 150)
	local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)
	
	local Part = Instance.new("Part", game.Workspace)
	Part.FormFactor = "Custom"
	Part.BrickColor = BrickColor.new("Really red")
	Part.Material = Enum.Material.Neon
	Part.Shape = Enum.PartType.Cylinder
	Part.Anchored = true
	Part.CanCollide = false
	
	local Distance = (Tip.CFrame.p - HitPosition).Magnitude
	Part.Size = Vector3.new(1,1, Distance)
	Part.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0,0, - Distance/2)
	local Humanoid = Tool.Parent:FindFirstChild("Humanoid")
	Humanoid.Health = Humanoid.Health + 40
	
	game.Debris:AddItem(Part, 0.7)
	
	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(30)
		end
	end
end)	
1 Like

The axis of the cylinder you want to point is the X axis and not the Z axis. CFrame.new() and CFrame.lookAt() make the Z axis face something. Create a function that makes the X axis face (I’ve done so below)

--[[
* Gets a CFrame where the X axis faces a targetPos
* @param {Vector3} pos
* @param {Vector3} targetPos
* @returns {CFrame}
]]
local function CFramePointRightSide(pos, targetPos)
	local directionToFace = (targetPos - pos).unit
	local worldUp = Vector3.new(0,1,0)
	local zAxisFace = directionToFace:Cross(worldUp)
	local yAxisFace = directionToFace:Cross(zAxisFace)
	return CFrame.fromMatrix(pos, directionToFace, yAxisFace, -zAxisFace)
end

Then replace this section in your code

CFrame.new(Tip.CFrame.p, HitPosition)

With this

CFramePointRightSide(Tip.CFrame.p, HitPosition)

image

It doesn’t seem to work It would only go to the side of the tool.

Part.CFrame = (CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0,0, - Distance/2)) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))

I change it around since it didn’t change the orientation but it won’t grow?

That’s because, in the case of mine, you need to change your CFrame.new(0,0, -Distance/2) to be CFrame.new(Distance/2,0,0) because you’re using the X axis now.

Make the part’s CFrame so that the front surface is looking at the Tip.

I did that aswell It did not work.

Sorry for late reply but I tried that out and in other ways but it did not work.

You’re not doing something correctly. Both his and my code works. Please provide a more reproduceable model for us to replicate what you are doing incorrectly.

I forgot to change the part size to the x axis instead of the z axis.