Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I go about making a distance delay for a gun using Raycast? [Sorry, no code included]

In big FPS's you see things such as Bullet delay, to give you a brief description (If you don't know what it is). Here's an example. One person is walking on the Z axis, and you're looking at them from the X axis, the person is moving from left to right, therefore, due to the bullet velocity and the users velocity you will have to aim more to the right in order for the bullet to hit the person (Depending on the distance). I know I can clone/create a brick with a velocity, but the hit detection is very glitchy due to the high velocity.

Any help/ideas would be appreciated,

Thanks :)

1 answer

Tried this but it fails with small targets at high velocities, still the main idea is just to check if there are parts along a path, traversing the path in a given speed.

function main()
	fireAt(Workspace.PartA.Position, Workspace.PartB.Position-Workspace.PartA.Position, 500, function(parts)
		print(parts[1].Name)
	end)
end

function _fireAt(origin, direction, speed, callback, size)
	direction = direction.unit
	size = size or 500
	--Debug part, to see the path, you can delete this 
	local Debug = true
	local debugPart = Instance.new("Part")
	debugPart.Anchored = true
	debugPart.Size = Vector3.new(1,1,1)
	debugPart.CanCollide = false
	debugPart.Parent = Workspace
	debugPart.Transparency = Debug and 0 or 1
	--
	local target = origin + (direction*size);
	for i = 0, size, speed/30 do
		local pos = origin:Lerp(target, i/size)
		local parts = Workspace:FindPartsInRegion3(Region3.new(pos, pos))
		--Can delete
		if Debug then
			debugPart.CFrame = CFrame.new(pos)
		end
		--
		if #parts > 0 then
			callback(parts)
		end		
		wait()
	end
	--Destroy the debug part, can delete.
	debugPart:Destroy()
	--
end

function fireAt(origin, direction, speed, callback, size)
	coroutine.wrap(_fireAt)(origin, direction, speed, callback, size)
end

main()

This discussion is preserved as it was. New answers, comments, and votes are closed.