Best way to ‘delay’ raycast?

Recently I’ve been working on a stationary shooting NPC. Everything works completely flawlessly, however the raycast is instant and there’s no way of dodging it. What’s the absolute best way of making him dodgable? I thought about comparing his torso position before and a little after the raycast was fired and if he’s moved significantly enough it’ll take damage, but I’m not sure about that and I want to know if there’s something better and easier.

1 Like

Then just Raycast at certain speed

How? Can you link me an article about that?

You could make a projectile that moves forward at a certain rate, perhaps bound to RunService.Heartbeat, then raycast forward a small distance to see if the projectile collides with the player at that instant.

I don’t know of article with this but all you need to do is apply the formula for calculating distance using speed, and then making the Ray depth the distance: here is a post I made on it in the past: Handling Railgun Bullets

I’m trying to go against the use of any parts whatsoever.

In this case, you know the start and end position that you are connecting with a ray. Think of this distance like a number line


You can raycast from point 1 to point 2 on this number line, wait a short time, raycast from point 2 to point 3, until you reach your final distance.

the only thing if you don’t show a projectile how will the player know to dodge it?

This is not a good approach if you want the rays to be casted at specific speed.

Wouldn’t this just delay the bullet still and give no possible outcome of dodging?

Rays cannot be casted at a “specific” speed. They are always instantaneous. The best way to simulate raycasting at a specific speed is to do multiple raycasts (along the same direction) with a short delay between them.

Just yield before the bullet is shot (raycast finished before yield)? Players would just have to move away from their last position within the yielded time frame to dodge the bullet, if they fail to do so, then they’re still at where the raycast finished at, then they’re damaged.

This could make it really easy for bullets to be dodged but you’d need to test to know plus you can just tinker with what you can do.

Yes you can cast them at a specific speed by doing multiple raycasts thats what i meant.

No that’s not the best way to do, the best way would be the one I linked in my post : Handling Railgun Bullets

I can’t even begin to fathom the algorithm or the math involved to cast multiple rays. How does this work?

You split the ray into smaller rays and go from one at a time continually until you’re too far, this would however be meant for moving projectiles if your bullet isn’t just an instant trail, basically it’d be for projectiles.

You can try doing a wait(sometime) before it, If you’d like another way then you can make the BodyVelocity method + raycast, its a good way to use like this since all you gonna need is make the speed the part will move to the location, and then when it hits the position (Second parameter of findpartonray or findpartonignorelist or findpartonwhitelist), it will destroy.

So formula for Speed is Distance/Time, so to get the distance you would transpose the equation and get Distance = Speed * Time, to get the time you can use the DeltaTime argument supplied from anyone of the RunService events.

local Start = --Where the ray should start at
local Target = --where the ray should end at
local Current = Start --the current origin for the ray
local Direction = (Target - Start).Unit
local Distance = (Target - Start).Magnitude
local RunConn

local SPEED = 50 --the speed at which the rays will get casted

RunConn = game:GetService("RunService").Heartbeat:Connect(function(DeltaTime)
     local Depth = SPEED * DeltaTime
     local Ray_ = Ray.New(Current, Depth * Direction)
     --cast the ray
     Current = Current + (Depth * Direction) --updating the origin of the ray cast to next point it should start at
     if (Current - Start).Magnitude > Distance then
         RunConn:Disconnect() --stop casting rays once the target is met