Happy holidays everyone!
I am making a placement system. I am casting a ray for the purpose of getting its normal to decide how to rotate the barrier which the player will be able to place down. However, sometimes the ray does not hit a part at one spot whilst it does at another.
The purple line is a visualization of the ray’s origin and where it hits.
I have looked reread the Raycast docs along with the section for Mouse.Hit, and tried skimming the forums here to identify the problem. I could not find anything.
Here is the script that changes the position of the Barrier model and casts the ray. Hopefully it isnt too messy ^^
local Player = game:GetService("Players").LocalPlayer
local ReplicatedStoraage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Model = workspace.Barrier
local Mouse = Player:GetMouse()
Mouse.TargetFilter = Model
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = Model:GetChildren()
table.insert(raycastParams.FilterDescendantsInstances,workspace.RAY)
print(raycastParams.FilterDescendantsInstances)
RunService.Heartbeat:Connect(function()
Model:PivotTo(CFrame.new(0,0,0.25) + Mouse.Hit.Position)
local raycastResult = workspace:Raycast(Mouse.Hit.Position + Vector3.new(0,5,0), Mouse.Hit.Position + Vector3.new(0,-10,0),raycastParams)
if raycastResult then
print("Instance:", raycastResult.Instance)
--print("Position:", raycastResult.Position)
--print("Distance:", raycastResult.Distance)
--print("Material:", raycastResult.Material)
--print("Normal:", raycastResult.Normal)
local distance = (Mouse.Hit.Position + Vector3.new(0,5,0) - raycastResult.Position).Magnitude
local p = workspace.RAY
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.new(Mouse.Hit.Position + Vector3.new(0,5,0), Mouse.Hit.Position)*CFrame.new(0, 0, -distance/2)
else
warn("No raycast result!")
end
local touching = false
for i, part in (Model:GetChildren())do
for ii, within in (workspace:GetPartsInPart(part))do
if within.Transparency ~= 1 and within.CanCollide == true and within.Parent ~= Model then
touching = true
end
end
end
if touching == true then
for i, part in (Model:GetChildren())do
part.Color = Color3.fromRGB(255, 0, 0)
end
else
for i, part in (Model:GetChildren())do
part.Color = Color3.fromRGB(0, 255, 0)
end
end
end)
Below, I have attached a screenshot of my workspace and a video. Ideally the video should explain my problem much better than I tried above.