You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve?
I am in the process of creating a simple Raycast gun. However, things aren’t going as planned.
- 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.
- 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.