Hi there!
I noticed that a significant proportion of my raycasts fail on objects whose position is tweened through the c0 of a Weld.
To be clear: I weld an unanchored part to an anchored part (such as the baseplate), and then tween the C0 property of that weld, causing the unanchored object to move.
However, while moving, Raycasts on the moving object go right through the object a proportion of the time. I can reproduce it consistently but the proportion sometimes changes for unknown reasons.
I cast the ray locally, and it doesn’t matter wether I tween on the server or client: in both cases the same issue occurs. The object is visible on the screen in the correct position, but does not (consistently) register in raycasts.
The problem occurs both in studio and in live games. I’m not sure when it started happening, I imagine forever, though I use this technique (tweening C0) a lot and never ran in to it.
I wrote the following script to demonstrate the issue. Put it in a LocalScript in a ScreenGui in StarterGui. It can also be found in open access place:
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
print(mouse.Target)
end)
local function weld(a, b, relOffset)
local myWeld = Instance.new("Weld")
myWeld.Part0 = a
myWeld.Part1 = b
myWeld.Parent = a
myWeld.C0 = relOffset:Inverse()
return myWeld
end
local testPart = Instance.new("Part")
testPart.Size = Vector3.new(15, 10, 20)
testPart.Anchored = false
testPart.Parent = game.Workspace
local baseplate = game.Workspace:WaitForChild("Baseplate")
local testWeld = weld(testPart, baseplate, CFrame.new(Vector3.new(0, 20, 0)))
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local target = CFrame.new(Vector3.new(0, 30, 10))
local tween = tweenService:Create(testWeld, tweenInfo, {C0 = target:Inverse()})
tween:Play()
If you run the script, and click on the part it creates, it should print “Part” but instead it often prints “Baseplate” or “nil” (or whatever is behind the part) instead.
Thank you for any help. I may be doing something wrong but I spent quite some time figuring out the problem. Any manual raycast also has the same problem as mouse.Target in my simple example.
Kind regards,
Nonaz_jr
p.s. I also tried with the new raycasts, but it had the exact same problem. WorldRoot | Roblox Creator Documentation