What do you want to achieve? Keep it simple and clear!
I want create a block system like skywars and minecraft. I already have a block system and all i need is a prediction system that detects if the mouse is in front of a block.
What is the issue? Include screenshots / videos if possible!
Video of issue:
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I looked for solutions and searched them up and found two forum posts. They were too hard to understand.
local camera = workspace.CurrentCamera
local players = game:GetService("Players")
local reps = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local player = players.LocalPlayer
local tool = script.Parent
local parts = reps:WaitForChild("Parts")
local outline = parts.Outline:clone()
local block = parts.Block
local mouse = player:GetMouse()
local reach = 100
local castParams = RaycastParams.new()
local result = nil
local result2 = nil
local pos = Vector3.new()
local equipped = false
local char = player.Character
while not char do
char = player.Character
wait()
end
local hrp = char:WaitForChild("HumanoidRootPart")
outline.Parent = workspace
outline.SelectionBox.Transparency = 1
local function snap(newcf)
local x = math.round(newcf.X/4)*4
local y = math.round(newcf.Y/4)*4
local z = math.round(newcf.Z/4)*4
return Vector3.new(x,y,z)
end
tool.Equipped:Connect(function()
equipped = true
outline.SelectionBox.Transparency = 0
end)
tool.Unequipped:Connect(function()
equipped = false
outline.SelectionBox.Transparency = 1
end)
mouse.Button1Down:Connect(function()
if equipped then
if result then
local newBlock = block:Clone()
newBlock.Position = Vector3.new(math.round(pos.x/4)*4,math.round(pos.y/4)*4,math.round(pos.z/4)*4)
newBlock.Parent = workspace
end
end
end)
rs.RenderStepped:Connect(function()
if equipped then
local unitRay = mouse.UnitRay
castParams.FilterDescendantsInstances = {player.Character,outline}
result = workspace:Raycast(unitRay.Origin,unitRay.Direction*reach,castParams)
if result then
pos = result.Position+result.Normal*2
outline.Position = Vector3.new(math.round(pos.x/4)*4,math.round(pos.y/4)*4,math.round(pos.z/4)*4)
elseif not result then
local camray = camera:ScreenPointToRay(mouse.X,mouse.Y)
local ray2 = Ray.new(camray.Origin,camray.Direction*1000)
local part1,pos1 = workspace:FindPartOnRayWithIgnoreList(ray2,{player.Character,outline},false,true)
local lv = CFrame.new(pos1*-1).LookVector*4
outline.Position = snap(lv)
end
end
end)
I have it working from the left and right sides but not the front and back. It’s also not really accurate and doesn’t detect the back, left, and bottom normals.
my code:
local camera = workspace.CurrentCamera
local players = game:GetService("Players")
local reps = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local player = players.LocalPlayer
local tool = script.Parent
local parts = reps:WaitForChild("Parts")
local outline = parts.Outline:clone()
local block = parts.Block
local lastblock = nil
local mouse = player:GetMouse()
local reach = 100
local castParams = RaycastParams.new()
local result = nil
local result2 = nil
local pos = Vector3.new()
local equipped = false
local deebug = reps:WaitForChild("Debug")
local r = deebug.Ray
local m = deebug.Mouse
r.Parent = workspace
m.Parent = workspace
local char = player.Character
while not char do
char = player.Character
wait()
end
local hrp = char:WaitForChild("HumanoidRootPart")
outline.Parent = workspace
outline.SelectionBox.Transparency = 1
local function snap(v3)
return Vector3.new(math.round(v3.x/4)*4,math.round(v3.y/4)*4,math.round(v3.z/4)*4)
end
local function debugg(rp,mp)
r.Position = rp
m.Position = mp
end
tool.Equipped:Connect(function()
equipped = true
outline.SelectionBox.Transparency = 0
end)
tool.Unequipped:Connect(function()
equipped = false
outline.SelectionBox.Transparency = 1
end)
mouse.Button1Down:Connect(function()
if equipped then
if result then
local newBlock = block:Clone()
newBlock.Position = snap(pos)
newBlock.Parent = workspace
end
end
end)
rs.RenderStepped:Connect(function()
if equipped then
local unitRay = mouse.UnitRay
castParams.FilterDescendantsInstances = {player.Character,outline,r,m}
result = workspace:Raycast(unitRay.Origin,unitRay.Direction*reach,castParams)
if result then
local tsf = mouse.TargetSurface
pos = result.Position+result.Normal*2
outline.Position = snap(pos)
mouse.TargetFilter = outline
lastblock = mouse.Target
elseif not result then
local tsf = mouse.TargetSurface
local dir = Vector3.FromNormalId(tsf)
outline.Position = snap(lastblock.Position+(dir*(Vector3.new(unitRay.Direction.X*2,1,1)))*4)
end
end
end)