Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 10 years ago

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

Log in to vote
0
Answered by
Destrings 406 Moderation Voter
10 years ago

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()
Ad

Answer this question