Raycast beams acting up

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I am in the process of creating a simple Raycast gun. However, things aren’t going as planned.

  1. What is the issue?

Everything about the weapon is fine. However, when standing close to the target, the Raycast beam shoots in random directions, as in this example.

  1. What solutions have you tried so far?

I’ve gotten no results from the DeveloperHub OR Forum about this issue. I’ve tried to make it so that the beam wouldn’t be visible if the distance between the gun and the Raycast instance was too low, but that didn’t work.

local BulletHolePosition = nil

local RandomVar  = 1

script.Parent.Parent.ShootEvent.OnServerEvent:Connect(function(player,mousepos)

	if player.Character.Humanoid.Health > 0 then

		RandomVar  += 1

		math.randomseed(os.time() + RandomVar)

		local RayCastParams = RaycastParams.new()
		RayCastParams.FilterDescendantsInstances = {player.Character}
		RayCastParams.FilterType = Enum.RaycastFilterType.Exclude
		local RaycastResultStep1 = workspace:Raycast(script.Parent.Parent.BulletHole.Position,(mousepos - script.Parent.Parent.BulletHole.Position)*10000,RayCastParams)
		local HitPart = nil
		local Model = nil
		local RayDistance = nil
		local MidPoint = nil
		local Part = nil
		local BulletHole = script.Parent.Parent.BulletHole
		local RaycastResult = nil


		if RaycastResultStep1 then

			BulletHolePosition = script.Parent.Parent.BulletHole.Position

			local RandomizerX = mousepos.X + math.random(-1,1)
			local RandomizerY = mousepos.Y + math.random(-1,1)
			local RandomizerZ = mousepos.Z + math.random(-1,1)

			local NewMousePos = Vector3.new(RandomizerX,RandomizerY,RandomizerZ)

			RaycastResult = workspace:Raycast(script.Parent.Parent.BulletHole.Position,(NewMousePos - script.Parent.Parent.BulletHole.Position)*300,RayCastParams)

			if RaycastResult then

				HitPart = RaycastResult.Instance

				RayDistance = (RaycastResult.Position - BulletHole.Position).Magnitude

				MidPoint = BulletHole.Position + (RaycastResult.Position - script.Parent.Parent.BulletHole.Position)/2

				if RayDistance > 7 then

					local Bullet = Instance.new("Part")
					Bullet.CFrame = CFrame.new(MidPoint, RaycastResult.Position)
					Bullet.Parent = game.Workspace.FiredBulletsFolder
					Bullet.Material = Enum.Material.Neon
					Bullet.BrickColor = BrickColor.new("New Yeller")
					Bullet.Anchored = true
					Bullet.CanCollide = false
					Bullet.CanQuery = false
					Bullet.Transparency = 0.4
					Bullet.Size = Vector3.new(0.1,0.1, RayDistance)

					task.wait(0.1)

					Bullet:Destroy()

				end

(This isn’t the entire code, but it’s the section that is acting up.) Any help would be appreciated.

2 Likes

The issue is this randomizer method.

The full explanation and one possible solution should be similar to this other post below which had the same issues as yours:

2 Likes

A rundown from this one as the referenced post makes it seem overcomplicated.

  1. The ray lengths are too long, Find the distance to the mouse and make it about that length (Prevents any misshaps and keeps things tidy)
  2. Use Angles for spread, its quite simple really, change randomizer xyz to be a vector 3 and randomise that vector 3 a bit (Play with it a bit to better understand it and find values you like) and you add that angle translate the original path, this means you take the ray from the origin with the angle being calculated to the target and simply add on that vector3 to the angles.
4 Likes

I’m new to raycasting, and I do not know how to put an angle translation to the path. Would you mind giving an example? I would really appreciate it.

1 Like
local RayCF = CFrame.new(Origin,Mouse.Hit.Position)
local Randomize =RayCF.FromOrientation(math.random(-5,5),math.random(-5,5),math.random(-5,5))

I looked at the api as I havent really used this myself, you can use FromOreintation to translate a cframe by a angle, translating it just means changing the angle, before you cast a ray its always a good idea to define a cframe that will be the raycast so you can play around with it before casting

1 Like

“FromOrientation is not a valid member of CFrame”. This is the error message I got.
Is “Origin” the position where the Raycast starts? If not, that might be the mistake I made.

Origin is the position of where the cframe starts, defined the same way most rays are so in this case the end of the gun, and from orientation could be a function :fromOrientation() im only going off the api at this point. If thats not it ill have a look in studio and pull something sililar to it off my boid script

Yep, it didn’t work with :FromOrientation either. However, the first solution you gave regarding the angles worked somewhat well.

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