How to set max distance that a raycast could go?

So, I am currently making a game where you drag and drop items (like cook burgers). I am currently trying to make a raycast that follows the player’s cursor but how would I make the raycast have a limit where it can’t go beyond 5 studs?

4 Likes

The ray will only travel as far as the distance’s magnitude is, so if you have a unit vector and multiply it by 5, the ray will only travel 5 studs.

4 Likes

This does work but when I mouse my cursor farther away from 5 studs, it just stops completely. It doesn’t follow the cursor in the Y axis.

I have spent a while of today learning ray casting but I still can’t do it.

2 Likes

Can you show the code you have made?

2 Likes
local players = game:GetService('Players')
local userInputService = game:GetService('UserInputService')
local runService = game:GetService('RunService')

local Items = workspace.Items -- can be any instance such as a model, folder, even a part if you really wanted it to
local mouse = players.LocalPlayer:GetMouse() -- get the player's mouse object

local function splitTitleCaps(str)
	str = str:gsub("(%u)", " %1")
	return str:gsub("^%s", "")

end

userInputService.InputBegan:Connect(function(key, isSystemReserved)
	
	if key.KeyCode == Enum.KeyCode.E or key.UserInputType == Enum.UserInputType.MouseButton1 then 
		local part = mouse.Target 
		if part then 
			if part:IsDescendantOf(Items) then 
				if part.Parent:IsA("Model") and part.Parent.BeingHeld.Value == "" then
					mouse.TargetFilter = Items
					
					-------------------------------------------------------------------------------
					local Player = game.Players.LocalPlayer
					local Char = Player.Character
					local PlayerGUI = Player.PlayerGui
					local Head = Char:FindFirstChild("Head")
					local HRP = Char:FindFirstChild("HumanoidRootPart")
					
					
					local Model = part.Parent
					-------------------------------------------------------------------------------
					local params = RaycastParams.new()
					params.FilterType = Enum.RaycastFilterType.Exclude
					params.FilterDescendantsInstances = {Player:GetDescendants(), Model:GetDescendants()}
					-------------------------------------------------------------------------------
					
					
				
					Model.LastHeld.Value = Player.Name
					game.ReplicatedStorage.Events.SendLastHeld:FireServer(Model.LastHeld)
					-------------------------------------------------------------------------------
					
					
					while userInputService:IsKeyDown(Enum.KeyCode.E) or userInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) do -- while the mouse button is pressed
							----------------------------------------------------------------
							Model.BeingHeld.Value = Player.Name
							game.ReplicatedStorage.Events.SendHeld:FireServer(Model.BeingHeld, Player.Name)
							----------------------------------------------------------------
						
							---------------Spliting Capitilized Letters---------------------
							local op = splitTitleCaps(Model.Name)
							
							PlayerGUI.HoldingGUI.TextLabel.Visible = true
							PlayerGUI.HoldingGUI.TextLabel.Text = "holding "..op
							----------------------------------------------------------------
							
							local vector = (mouse.Hit.p - Head.Position).Unit * 20 --Wanted to extend it a little to test.
							local Raycast = game.Workspace:Raycast(Head.Position, vector, params)
							
							if Raycast then
								Model:PivotTo(CFrame.new(Raycast.Position))
								print(Raycast.Distance) 
								Model:PivotTo(Model:GetPivot() * CFrame.new(0, part.Size.Y / 2, 0)) 
								
								game.ReplicatedStorage.Events.SendPosition:FireServer(Model, Model:GetPivot())
							end
							
						task.wait() 
					end
					
					
				-----------------------Once the player has stopped holding: -----------------
				-----------------------------------------------------------------------------
					Model.PrimaryPart.PivotOffset = CFrame.new(0,0,0)
					game.ReplicatedStorage.Events.SendPivotOffset:FireServer(Model, CFrame.new(0,0,0))
					
					PlayerGUI.HoldingGUI.TextLabel.Visible = false
					game.ReplicatedStorage.Events.SendHeld:FireServer(Model.BeingHeld, "")
				-----------------------------------------------------------------------------
				end
			end
		end
	end
end)

1 Like

Replace this code:

if Raycast then
	Model:PivotTo(CFrame.new(Raycast.Position))
	print(Raycast.Distance) 
	Model:PivotTo(Model:GetPivot() * CFrame.new(0, part.Size.Y / 2, 0)) 
								
	game.ReplicatedStorage.Events.SendPosition:FireServer(Model, Model:GetPivot())
end

with this:

local Position = Raycast and Raycast.Position or (Head.Position + vector)
Model:PivotTo(CFrame.new(Raycast.Position + Vector3.yAxis * part.Size.Y / 2))
					
game.ReplicatedStorage.Events.SendPosition:FireServer(Model, Model:GetPivot())

Only problem with it generally is that it relies on Mouse.Hit, which can sometimes be nothing(for example, when looking at the sky). I recommend you switch to just using the Camera’s CFrame and it’s LookVector for the Raycast, alternatively if you want to support third person, you can use Camera:ScreenPointToRay() with the mouse’s X and Y position. Nevermind, this isn’t the case.

2 Likes

I think you made a mistake in that line. Did you mean Position instead of Raycast.Position?

3 Likes

It worked, tysm @AlaTomKing :wink: & @XSiggeManx

1 Like

Mark his answer as the solution, he’s the real MVP.

2 Likes


It still keeps on teleporting when I point it to either the top or bottom. I find this a little annoying, I am not sure what is causing it.

1 Like

Did you use the RaycastParams to exclude yourself, the character?

1 Like

oh yeah, I made a mistake on my part, I decided to exclude the player instead of the character :rofl: :rofl: :sweat_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.