So for a while now, I’ve been trying to make a simple “module” that can be used for a good range of magic-type projectiles and abilities that uses the world position of attachments for raycasting and hit detection. Yes, I know that there are modules out there that can already do this, but I feel like most of them are too specific (e.g. fastcast is specialized for guns and small, fast projectiles) or are simply overkill for my purpose.
In my module, I’ve managed to get a lot of things working, including the raycasting, however there is one small issue. The faster the attachment moves, the further away from the hitbox the rays will begin and end. It’s hard to explain, so I linked a couple of videos (I apologize for the quality🗿):
(The origin of the ray is the cyan blue part, the direction of the ray is the bright red part, and the destination of the part is the light green part. The green circles are the attachments themselves)
In the first video, the projectile had a velocity of 1 and the origin was right on top of the attachment, and the destination part was right in front. This is how I want it, but in the next video, the projectile was moving at a velocity of 3 and the ray was just slightly in front of the attachment. This isn’t too bad, but in the last video, the projectile had a velocity of 60 (the speed I want) and the ray was far ahead of the projectile, which leads to the ray detecting an object prematurely. I don’t yet know if this has anything to do with this, but everything is done on the server (and yes I know VFX is bad on the server, I’m working on moving it to the client). Does anybody know how this could be fixed?
For those who want to see the code:
if object:IsA("BasePart") then
object:SetNetworkOwner(nil)
partOrigin = object.Position
object.CFrame = CFrame.lookAt(object.Position, mouseCFrame.Position)
local mainAttachment = object:FindFirstChild("Main")
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.MaxForce = 100000000
linearVelocity.Attachment0 = mainAttachment
linearVelocity.VectorVelocity = (mouseCFrame.Position - object.Position).Unit * velocity
linearVelocity.Parent = mainAttachment
local alignOrientation = Instance.new("AlignOrientation")
alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
alignOrientation.Attachment0 = mainAttachment
alignOrientation.RigidityEnabled = mainAttachment
alignOrientation.CFrame = mouseCFrame
alignOrientation.Parent =mainAttachment
eventConnection = runService.Heartbeat:Connect(function()
if (object.Position - partOrigin).Magnitude > maxDistance then
hitHandled(nil)
end
for i, attachment in ipairs(object:GetChildren()) do
if attachment.Name == "RayPoint" and attachment:IsA("Attachment") then
local rayOrigin = attachment:GetAttribute("LastPosition") or attachment.WorldPosition
local rayDestination = attachment.WorldPosition
local rayDirection = rayDestination - rayOrigin
local rayCastResult = workspace:Raycast(rayOrigin, rayDirection, fireParams)
if rayCastResult then
local hitObject = rayCastResult.Instance
print(hitObject)
if hitObject:IsDescendantOf(workspace:WaitForChild("Passthrough")) or hitObject:IsDescendantOf(workspace:WaitForChild("AbilityFX")) then return end
hitHandled(rayCastResult)
end
local visualizedRay: BasePart = visualizeRay(rayOrigin, rayDirection, rayDestination)
debris:AddItem(visualizedRay, .04)
attachment:SetAttribute("LastPosition", attachment.WorldPosition)
end
end
end)
end