Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the part going in both direction and not just one when shot?

I'm not completely sure this is a Raycasting issue but I'm pretty new to it, so I'm trying to make a gun but im trying to get the basics of all the components before I put them all together, so I made a simple 2 part gun just for testing and it all works fine because right now I just want it so when you click, it goes from the gun to the mouse position, That works a little fine, it goes but not all the way, but also the bullet goes in both directions and not just the one way I want it to go.

Gun.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		Shooting = true
		print("Gun started shooting")
		while Shooting == true do 
			wait(.01)
			if Shooting == true then
				local newRay = Ray.new(script.Parent.Barral.CFrame.p, (mouse.Hit.p - script.Parent.Barral.CFrame.p))
				local part,position = game.Workspace:FindPartOnRay(newRay,game.Players.LocalPlayer.Character,false,true)
				local Part = Instance.new("Part")
				Part.Parent = game.Workspace
				Part.CanCollide = false
				Part.Anchored = true
				Part.Parent = game.Workspace.Welds
				Part.Anchored = true
				Part.CanCollide = false
				local distance = (script.Parent.Barral.CFrame.p - position).magnitude
				Part.Size = Vector3.new(.3,.3, distance)
				Part.CFrame = CFrame.new(script.Parent.Barral.CFrame.p, position) * CFrame.Angles(0,0, -distance/2)
			script.Parent.RemoteFunction:InvokeServer()
			else
				
				end
		end
	end)
	mouse.Button1Up:Connect(function()
		Shooting = false
		print("Gun stopped shooting")
	end)
end)

There's my script, again pretty new to Raycasting so any help would be great, thank you!

1 answer

✓ Accepted answer

Try replacing newray with this

local newRay = Ray.new(script.Parent.Barral.CFrame.p, (mouse.Hit.p - script.Parent.Barral.CFrame.p).Unit * 70) --the 70 is the range

8 comments

0

Now the size is becoming a little bigger than 2 studs.

0

Sorry wait didn't see the thing you added.

0

It goes in both directions again, and without the 70 or whatever range, it goes like 2 studs.

0

Change the CFrame.Angles to CFrame.new and see if it changes anything

0

Yes! Thank you! I have one more question though, what does .Unit do? I've been trying to find out what it does but I've had no luck.

0

It returns the direction its facing

0

its as in what your trying to get the direction of

0

Oh alright, thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.