Gun's ray part going up each fire

  1. What do you want to achieve? Keep it simple and clear!

Simple, the Ray must shoot straight and that’s it.

  1. What is the issue? Include screenshots / videos if possible!

In the game, the ray’s end direction is going up (LOOK AT THE MOUSE CURSOR CLOSELY)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I don’t know. The code are simple.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function ShootFunction(Player, MouseHit)
	if Reloading then 
		return
	end

	if Ammo.Value == 0 then
		ReloadFunction()
		return
	else
		local rayOrigin = Weapon.Muzzle.CFrame.p
		local maxDistance = 2000 -- Maximum distance for straight shooting

		local direction = (MouseHit - rayOrigin).Unit

		local ray = Ray.new(Weapon.Muzzle.CFrame.p,(MouseHit - Weapon.Muzzle.CFrame.p).Unit * maxDistance)
		local ignoreList = { Weapon.Parent, workspace.RayContainer }

		local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)

		if hit then
			local hum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
			if hum then
				hum:TakeDamage(11)
			end
		end

		Ammo.Value -= 1

		local Range = (position - Weapon.Muzzle.Position).Magnitude

		local Ray = Instance.new("Part")
		Ray.Size = Vector3.new(0.2, 0.2, Range)
		Ray.CFrame = CFrame.new(position, Weapon.Muzzle.CFrame.p) * CFrame.new(0, 0, -Range / 2)
		Ray.Anchored = true
		Ray.CanCollide = false
		Ray.Parent = workspace.RayContainer
		Ray.Color = Color3.fromRGB(255, 255, 127)
		Ray.Material = Enum.Material.Neon

		FireSound:Play()

		local Goal = {}
		Goal.Transparency = 1
		Goal.Size = Vector3.new(0.05, 0.05, Range)

		local TweenInfo = TweenInfo.new(0.2)
		local TweenRay = Tween:Create(Ray, TweenInfo, Goal)
		TweenRay:Play()

		ShootAnim:Play()

		Debris:AddItem(Ray, 0.2)
	end
end

Ok that’s it. Thanks a lot.

2 Likes

It happens because MouseHit hits the beam of the last bullet. You need to turn off CanQuery on the tracer/bullet part.

But they’re ignoring it. So that can’t be the case.

The Raycast from the muzzle to the Hit is ignoring it, yes. But LocalPlayer:GetMouse().Hit is not ignoring it. Just try inserting this line:
Ray.CanQuery = false
somewhere along here:

local Ray = Instance.new("Part")
Ray.Size = Vector3.new(0.2, 0.2, Range)
Ray.CFrame = CFrame.new(position, Weapon.Muzzle.CFrame.p) * CFrame.new(0, 0, -Range / 2)
Ray.Anchored = true
Ray.CanCollide = false
Ray.Parent = workspace.RayContainer
Ray.Color = Color3.fromRGB(255, 255, 127)
Ray.Material = Enum.Material.Neon

Nice one it works.

Btw, I don’t know what does CanQuery do?

It determines if the Part will be considered during “spatial query operations” like Raycasts and GetPartsInPart. Disabling it will make all Raycasts and such ignore it.

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