How to use Raycasting for this

Hello developers

i’m making a cooking system and with some help i got a part of the work done of the dragging system.
this is what currently i have

The next thing that i need is checking with Raycasting the position to land the ingredients.
this is what i mean:

The red line would be the Ray, i want the ingredients to fall to the end of the ray and get on the table.

this is the code:


--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CurrentCamera = workspace.CurrentCamera

--//Controls
local Point = nil
local Clicking = false
local tweenInfo = TweenInfo.new(0.1)

--//Initialization
if game.Loaded then
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	CurrentCamera.CFrame = workspace.CamaraMostrador.CFrame
end

--//Functions
Mouse.Button1Up:Connect(function()
	Clicking = false
	Point = nil
	Mouse.TargetFilter = nil
end)

Mouse.Button1Down:Connect(function()
	if Mouse.Target and not Mouse.Target.Locked and Mouse.Target:IsA("BasePart") and Mouse.Target:FindFirstChild("Value") then
		Point = Mouse.Target
		Mouse.TargetFilter = Point
		Clicking = true
	end
end)

Mouse.Move:Connect(function()
	if Clicking and Point then
		local TweenAnimation = TweenService:Create(Point, tweenInfo, {Position = Mouse.Hit.Position + Vector3.new(0, Point.Size.Y * 2, 0)})
		TweenAnimation:Play()

		--[[
		Point.Position = Vector3.new(PosX,PosY,PosZ)
		]]
	end
end)

I make this topic because i don’t know about raycasting

Thank you! :slight_smile:

Also if you got any tutorial for checking collisions it would be really cool

If we want to cast a ray from the food to the table we will need their positions. We will also have to do a little bit of math.

The basic syntax of raycast is:
workspace:Raycast(origin, direction, parameters)

Origin and direction are Vector3 values while the parameters are represented using RaycastParams.new().

Origin: Food position.
Direction: (Food position - Vector3.new(0,1,0)) * distance
Parameters: Whitelist only the table.

Here we are casting the ray straight down from the food using the food’s position and going towards negative Y.

Here is some hypothetical code:

local food = ...
local surface = ...

local params = RaycastParams.new()
params.FilterDescendantsInstances = {surface}
params.FilterType = Enum.RaycastFilterType.Whitelist

local function CastRay(a:Vector3, b:Vector3, d:number)
	local result = workspace:Raycast(a, b.Unit * d, params)
	if result then 
		return result
	end
end

local result = CastRay(food.Position, food.Position - Vector3.yAxis, 20)

Simply call the CastRay function with the origin, direction, and max distance parameters.

We can check if the raycast worked by doing:

if result then
    -- Raycast worked
end

Check the raycast’s hit position with:

local pos = result.Position

Check what the raycast hit with:

local hit = result.Instance

Hope this helps! :slight_smile:

With that code the food goes to the left, i tried doing my own


--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CurrentCamera = workspace.CurrentCamera

--//Controls
local Point = nil
local Clicking = false
local tweenInfo = TweenInfo.new(0.1)

--//Initialization
if game.Loaded then
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	CurrentCamera.CFrame = workspace.CamaraMostrador.CFrame
end

--//Functions


Mouse.Button1Up:Connect(function()
	
	Clicking = false
	
	if Point then
		
		local RayParams = RaycastParams.new()
		RayParams.FilterDescendantsInstances = {game.Workspace.Mostrador,game.Workspace.Baseplate}
		RayParams.FilterType = Enum.RaycastFilterType.Blacklist
		
		local NewRay = Ray.new(Point.Position,Point.CFrame.YVector * -3)
		local Result = workspace:Raycast(NewRay.Origin,NewRay.Direction)
		
		local MidPoint = NewRay.Origin + NewRay.Direction/2
		
		--local Part = Instance.new("Part")
		--Part.Parent = game.Workspace
		--Part.Anchored = true
		--Part.CanCollide = false
		--Part.CanTouch = false
		--Part.CanQuery = false
		--Part.BrickColor = BrickColor.new("Really red")
		--Part.Transparency = 0.6
		--Part.Size = Vector3.new(0.1,0.1,NewRay.Direction.Magnitude)
		--Part.CFrame = CFrame.lookAt(MidPoint,NewRay.Origin)
		
		local TSinfo = TweenInfo.new(0.2)
		
		local TsAnim = TweenService:Create(Point,TSinfo,{Position = Result.Position})
		TsAnim:Play()

		
		--Point.Position = Result.Position

	end
	
	Point = nil
	Mouse.TargetFilter = nil
	
end)

Mouse.Button1Down:Connect(function()
	if Mouse.Target and not Mouse.Target.Locked and Mouse.Target:IsA("BasePart") and Mouse.Target:FindFirstChild("Value") then
		Point = Mouse.Target
		Mouse.TargetFilter = Point
		Clicking = true
	end
end)

Mouse.Move:Connect(function()
	if Clicking == true and Point then
		local TweenAnimation = TweenService:Create(Point, tweenInfo, {Position = Mouse.Hit.Position + Vector3.new(0, Point.Size.Y + 0.5, 0)})
		TweenAnimation:Play()

		--[[
		Point.Position = Vector3.new(PosX,PosY,PosZ)
		]]
	 end
end)


This one works well but the problem is that the collisions aren’t perfect, the parts can pass the other parts, is there any way to make the collisions better so it lands perfect?

This is a better example