How to do raycasting with limits?

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 >.<

1 Like
if (MouseVector - Handle.Position).Magnitude > Range then
     MouseVector = Handle.Position + (MouseVector - Handle.Position).Unit * Range
end

Put this after you define MouseVector and it will move it within the range.

4 Likes

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)

My guess is that the ray only goes to Mouse.Hit.p which is on the surface of the part, so the ray doesn’t go far enough to actually hit the part.

You can lengthen the ray a little bit like this, to test if that’s actually the problem.

MouseVector = MouseVector + MouseVector.Unit * 0.5

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

See if this fixes that: replace * range with * (range + 0.01)

isnt that what @TriplePattyBurger suggested already though? Just extending the ray?

I see the problem now. The second argument in Raycast should be the direction, not the target. So it should be MouseVector - Handle.Position.

It would then be more efficient to define MouseVector as Mouse.Hit.p - Handle.Position. The if statement changes to

if MouseVector.Magnitude > Range then
     MouseVector = MouseVector.Unit * Range
end

Edit: Redefining MouseVector messes up other parts of the code so just do the first paragraph.

That doesnt seem to help it out, it makes the ray completely off target now :o

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.