I’m creating a projectile system for decently fast moving projectiles, I want to use raycasting because it’s the most efficient form of hit-detection I am aware of, but I also wanna use LinearVelocity to move the object because it’s more flexible vs moving it by position, as I am looking to be able to deflect the projectiles, slow them down etc… and this can easily be achieved from experience with a body mover, but I wanna know the best way to combine the two, here’s what I currently have:
function Projectile:Fire()
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = self.IgnoreList
Params.FilterType = Enum.RaycastFilterType.Exclude
local Velocity = self:ApplyVelocity(self.Speed)
if Velocity then
CONNECTION = game:GetService("RunService").Heartbeat:Connect(function(dt)
local Hit = workspace:Raycast(self.Origin, self.Model.CFrame.LookVector * (50/dt), Params)
if Hit then
print("Hit")
Velocity.VectorVelocity = Vector3.new(0,0,0)
CONNECTION:Disconnect()
else
print("not hitting")
end
end)
end
end
function Projectile:ApplyVelocity(SPEED)
local Attachment = Instance.new("Attachment")
Attachment.Parent = self.Model
local Velocity = Instance.new("LinearVelocity")
Velocity.MaxForce = 10000
Velocity.Name = "ProjectileForce"
Velocity.Attachment0 = Attachment
Velocity.VectorVelocity = CFrame.new(self.Origin, self.Direction).LookVector * SPEED
Velocity.Parent = self.Model
return Velocity
end