I'm trying to figure out a few things about a gun turret. Can someone help me?
Asked by
6 years ago Edited 6 years ago
I'm making a gun turret to practice my raycasting abilities, and I am following a tutorial, but I have a problem. A few, actually. First, and biggest, in the findTarget function the script generates a ray, and multiplies it by 500 with a .Unit . What does that do? Second, I want to use that ray to make sure that its not firing at a wall the humanoid is hiding behind, but instead, the humanoid itself. Third, how would I add inaccuracy to my gun? Fourth, the FireBullet() function works, but how do I make the sound sound like its overlapping, it doesn't sound right, its sounds glitchy. Heres the code:
06 | for i,v in pairs (workspace:GetChildren()) do |
07 | local torso = v:FindFirstChild( "Torso" ) |
08 | local humanoid = v:FindFirstChild( "Humanoid" ) |
09 | if torso and humanoid and humanoid.Health > 0 then |
10 | if (gun.Position - torso.Position).magnitude < dist then |
11 | local ray = Ray.new(gun.Position, (gun.Position- torso.Position).Unit* 500 ) |
12 | local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, { torso.Parent } ) |
14 | dist = (gun.Position - torso.Position).magnitude |
19 | dist = (gun.Position - torso.Position) |
28 | s = workspace.Sound:Clone() |
29 | s.Parent = script.Parent |
36 | local torso = FindTarget() |
38 | gun.CFrame = CFrame.new(gun.Position, torso.Position) |
39 | local b = Instance.new( "Part" ,workspace) |
40 | b.Size = Vector 3. new( 0.2 , 0.2 , 1 ) |
41 | b.BrickColor = BrickColor.new( "New Yeller" ) |
42 | b.Velocity = gun.CFrame.lookVector* 500 |
43 | b.CFrame = gun.CFrame + Vector 3. new( 0.2 , 0.2 , 0.2 ) |
44 | b.Touched:connect( function (obj) |
46 | local human = obj.Parent:FindFirstChild( "Humanoid" ) |
51 | game:GetService( "Debris" ):AddItem(b, 0 ) |