Laser Gun Beam Not Shooting Correct Direction?
Hello developers. I am currently making a laser gun with a long laser beam that shoots out when you click. However, the laser beam doesn't go straight, but it goes to the center of the object I am shooting at. I was wondering how I could make the laser beam go straight no matter what is in the way and so it doesn't just go to the center of the object.
Here is the local script located in the laser gun:
01 | function CreateBullet(Origin, Direction) |
02 | local MidPoint = Origin.Position + Direction/ 2 |
04 | local beam = Instance.new( "Part" , nil ) |
05 | beam.Name = "LaserBullet" |
07 | beam.CanCollide = false |
10 | beam.Parent = workspace |
11 | beam.BrickColor = Player.TeamColor |
12 | beam.Material = Enum.Material.Neon |
14 | beam.Size = Vector 3. new( 0.05 , 0.05 , Direction.Magnitude) |
15 | beam.Orientation = Origin.Orientation |
16 | beam.CFrame = CFrame.new(MidPoint, Origin.Position) |
20 | Debris:AddItem(beam, 1 ) |
22 | beam.Touched:Connect( function (Result) |
23 | if Result and Result.Parent:FindFirstChild( "Humanoid" ) and hitDebounce = = false then |
25 | local humanoid = Result.Parent:FindFirstChild( "Humanoid" ) |
27 | if humanoid and humanoid ~ = Player.Character.Humanoid then |
28 | if Result.Name = = "Head" then |
29 | humanoid:TakeDamage(ConfigScript.CriticalDamage) |
31 | humanoid:TakeDamage(ConfigScript.NormalDamage) |
40 | beam.Transparency = beam.Transparency + . 1 |
46 | if not ShootCooldown and script.Parent.CurrentAmmo.Value > 0 and ReloadDebounce = = false then |
48 | script.Parent.CurrentAmmo.Value - = 1 |
49 | if GunScreen.Visible = = true then |
50 | GunScreen.Screen.Ammo.Text = "Ammo: " .. tostring (gun.CurrentAmmo.Value).. "/" .. tostring (gun.MaxAmmo.Value) |
51 | if ReplicatedStorage.Settings.AutoReload.Value = = true and script.Parent.CurrentAmmo.Value = = 0 then |
55 | if Player.Character:FindFirstChild(script.Parent.Name) then |
56 | local sin = math.sin(tick()*RecoilSize)/RecoilSize |
57 | CF = CF:Lerp(CFrame.new(ConfigScript.OffsetFromCamera.X, ConfigScript.OffsetFromCamera.Y,ConfigScript.OffsetFromCamera.Z+sin),. 1 ) |
58 | ReplicatedStorage.LaserGunShootingSound:Play() |
59 | local Origin = gun.Origin |
60 | local Direction = (Mouse.Hit.Position - Origin.Position).Unit * ((Mouse.Hit.Position - Origin.Position).Magnitude) |
61 | CreateBullet(Origin, Direction) |
62 | wait(ConfigScript.Cooldown_Between_Each_Click) |
65 | elseif script.Parent.CurrentAmmo.Value < = 0 then |
66 | GunScreen.Screen.Loaded.Text = "Must Reload!" |
67 | ReplicatedStorage.ErrorSound:Play() |
68 | ReplicatedStorage.ErrorSound.Stopped:Wait(. 8 ) |
Thanks for reading!