I’m making an FPS game and I made bullets and raycast system. Raycast detects if something is hit and deals damage to it, while bullet is just there for the looks.
-- this is how the bullet is moving
local velocity = Instance.new("BodyVelocity")
velocity.Velocity = bullet.CFrame.LookVector * bulletSpeed
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
velocity.Parent = bullet
-- this is how the raycast is created
local ray = workspace:Raycast(startCFrame, startCFrame.LookVector * bulletSpeed, raycastParams)
(this is not the exact code, it’s just an example)
My question is if it is possible to synchronize the bullet speed and raycast speed so they hit the target at the same time?
I haven’t worked with raycasts much, it was just a suggestion really. You should play around with it. Maybe if the bullet speed is fast enough you could just do the raycast immediately and check it, since it won’t matter whether the bullet actually hits it or not because of the moving delay
I actually managed to make it work by creating the raycast to instantly hit the target and then waiting for how long it takes a bullet to get to the target. I did that by using a simple formula:
velocity = distance / time => time = distance / velocity
local ray = workspace:Raycast(startCFrame, startCFrame.LookVector * bulletSpeed, raycastParams)
if ray then
wait(ray.Distance / bulletSpeed)
bullet:Destroy()
end