So, this is my first time doing raycasting and I’m pretty new to this stuff. For now, I just want to create a part that is limited in length, but goes in the direction of where the mouse is clicked. Here is my script currently:
Code
local Handle = script.Parent.Handle
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Range = 5
script.Parent.Activated:Connect(function()
local MouseVector = Mouse.Hit.p
local AddedRange = MouseVector+Vector3.new(0,0,Range)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local Raycast = workspace:Raycast(Handle.Position,MouseVector,raycastParams)
local PartCreated = Instance.new("Part")
PartCreated.Size = Vector3.new(.3,.3,(AddedRange-Handle.Position).magnitude)
PartCreated.CFrame = CFrame.new(Handle.CFrame.p,MouseVector)*CFrame.new(0,0,-((AddedRange-Handle.Position).magnitude/2))
PartCreated.Anchored = true
PartCreated.CanCollide = false
PartCreated.Parent=workspace
end)
I attempted to achieve this with the “AddedRange” Varible, but that just offset the part from my mouse. Currently, what this code achieves is the following:
There is still some offsetting happening, however, the length is endless depending on where I click.
I’ve studied the lua wiki page for a couple hours now and cannot for the life of me figure this out >.<
Thanks , however there still seems to be an issue. I think the raycasting is a bit offset especially when it hits a humanoid:
as you can see the ray is clearly hitting the dummy, but instead of printing the full name of the part it touched, it grants an error meaning it hasnt collided with anything.
New code:
Code
local Handle = script.Parent.Handle
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Range = 10
script.Parent.Activated:Connect(function()
local MouseVector = Mouse.Hit.p
if (MouseVector - Handle.Position).Magnitude > Range then
MouseVector = Handle.Position + (MouseVector - Handle.Position).Unit * Range
end
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local Raycast = workspace:Raycast(Handle.Position,MouseVector,raycastParams)
local PartCreated = Instance.new("Part")
PartCreated.Size = Vector3.new(.3,.3,(MouseVector-Handle.Position).magnitude)
PartCreated.CFrame = CFrame.new(Handle.CFrame.p,MouseVector)*CFrame.new(0,0,-((MouseVector-Handle.Position).magnitude/2))
PartCreated.Anchored = true
PartCreated.CanCollide = false
PartCreated.Material = Enum.Material.SmoothPlastic or Enum.Material.Neon
PartCreated.Color = Color3.fromRGB(123,0,123)
PartCreated.Parent=workspace
Debris:AddItem(PartCreated,1)
print(Raycast.Instance:GetFullName())
end)
This still produces an error, I have also added a part at the end point of the raycast to see where the ray ends up and it does look like it intercepts with the part
Ignore everything except the first paragraph. You shouldn’t actually redefine MouseVector because it is used correctly elsewhere. You just have to change the second argument of Raycast.