How to make Raycast clone part to surface instead of its center?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want that when activating the tool, raycast cloned part to the surface, and not to the center of the part, i.e. mouse click position

  2. What is the issue? watch the video

  3. What solutions have you tried so far? i tried to find my issue on devforum but i didnt found anything helpful

-- local script
local tool = script.Parent
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local event = tool:WaitForChild("RemoteEvent")

debounce = true

tool.Activated:Connect(function()
	if debounce then
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {tool.Parent}
		params.FilterType = Enum.RaycastFilterType.Blacklist 
		
		local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y, 0)
		local origin = tool.Handle.Position
		local direction = unitRay.Direction * 25
		local result = workspace:Raycast(origin, direction, params)
		
		if result ~= nil then
			debounce = false
			local pos = result.Position
			event:FireServer(pos, tool)
		end
	end
end)
--server script
local event = script.Parent:WaitForChild("RemoteEvent")
local rope = game:GetService("ReplicatedStorage").Rope

event.OnServerEvent:Connect(function(player, pos, tool)	
	local clone = rope:Clone()
	clone.Position = pos
	clone.Parent = game.Workspace.debris

	tool:Destroy()
end)

thank you

Just add half of it to top so:

ActualPos = Pos + Vector3.new(0,Your part.Size.Y/2,0)
--Add this below your Pos Variable and reference your part.

Place an attachment on your placed part to act as the “grip point”. When you place the part, in order to have the grip point be exactly on the surface, the following has to be true:
partCFrame * gripPointCFrame = placementCFrame
(gripPointCFrame is the Attachment.CFrame)

You can rearrage this as:

partCFrame = placementCFrame * gripPointCFrame:Inverse()

Note that the placement location is a CFrame whereas its probably a position in your case, just create a new CFrame that has your desired position and default rotation.

Now wherever you place the grip point will be where the part touches the placement surface. You can have this factor in rotation too but its annoying and if you wait long enough I will publish an example of how to do it.

1 Like

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