Now, don't get me wrong, because the Raycast tutorial, and even making a gun, was fantastic. However, I didn't really feel if it explained anything besides the basics, and when it did, it wasn't enough. The final product for the gun is this:
local Tool = script.Parent
local User
Tool.Equipped:connect(function(mouse)
User = Tool.Parent
mouse.Button1Down:connect(function()
local Ray = Ray.new(Tool.Handle.CFrame.p, (mouse.Hit.p - Tool.Handle.CFrame.p).unit*300)
local Hit, Position = game.Workspace:FindPartOnRay(Ray, User)
if Hit then
if Hit.Parent:FindFirstChild("Humanoid") then
Hit.Parent.Humanoid:TakeDamage(30)
end
end
local RayPart = Instance.new("Part", User)
RayPart.Name = "Lazer"
RayPart.BrickColor = BrickColor.new("Bright red")
RayPart.Transparency = 0.5
RayPart.Anchored = true
RayPart.CanCollide = false
RayPart.BottomSurface = Enum.SurfaceType.Smooth
RayPart.formFactor = Enum.FormFactor.Custom
local Distance = (Position - Tool.Handle.CFrame.p).magnitude
RayPart.Size = Vector3.new(0.2, 0.2, Distance)
RayPart.CFrame = CFrame.new(Position, Tool.Handle.CFrame.p) * CFrame.new(0, 0, -Distance/2)
game.Debris:AddItem(RayPart, 0.1)
end)
end)
,and I do not have issues with it. But I did have some questions on what did what. For starters, were exactly did
"Position" come from? I found it in this line here:
local Distance = (Position - Tool.Handle.CFrame.p).magnitude
I remember assigning it a value, but it was this:
local = Hit, Position = game.Workspace:FindPartOnRay(Ray, User)
What? Ok, I get that the ray uses the ray itself, and to ignore the user(me), but why is it apart of the same local as Hit, for which all I know, does not have a value and must take data later on in the script?
The second question, take a look at this line here:
RayPart.CFrame = CFrame.new(Position, Tool.Handle.CFrame.p)
What is p? Why could I not change it, such as adding a + 3 at the end of it.
Is it position? Is it a name for a Vector3 Value? I'm so confused.
Lastly, I promise, whats the deal with adding a times 300 in the line:
local Ray = Ray.new(Tool.Handle.CFrame.p, (mouse.Hit.p - Tool.Handle.CFrame.p).unit*300)
Yes, I know it works, I just don't know why. If I didn't have it there, would the lazer not be long enough?
Raycasting is so confusing at first... |