Is it possible to Raycast while using LinearVelocity to move the object?

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

I was able to get a raycast going, but the issue I face is where it prints hit when the projectile spawns, and continues to print not hitting as it keeps moving

if Velocity then
		CONNECTION = game:GetService("RunService").Heartbeat:Connect(function(dt)
			local Hit = workspace:Raycast(self.Origin, self.Model.CFrame.LookVector * 1, Params)

			if Hit then
				print("Hit")
				Velocity.VectorVelocity = Vector3.new(0,0,0)
				CONNECTION:Disconnect()
			else
				print("not hitting")
			end
		end)
	end

I actually was able to figure this out :sob: If you see this post please still give suggestions on how I can do some stuff better but I think I answered my own question

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.