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

Is this an efficient way to handle bullet travel on the server?

Asked by 3 years ago

Using raycasting every frame on the server, I'm aware this completely ignores the size of the bullet. If I wanted to incorporate that would it be wise to just unanchor the bullet and use velocity with a touch listener? Or I could incorporate EgoMoose's Region3 module. Any ideas?

---@param origin CFrame
    ---@param whitelist table<int, Instance> 
    function Class:Launch(origin, whitelist)
        local start = tick()
        local raycastParams = RaycastParams.new()
        raycastParams.FilterDescendantsInstances = whitelist or {}
        raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
        raycastParams.IgnoreWater = true

        local result = nil
        local yVelocity = 0
        while tick() < start + self.Lifetime and not result do
            local step = RunService.Stepped:Wait()
            local gravity = (workspace.Gravity / 196.2) * 9.807
            local drop = (gravity / 2) * math.pow(step, 2)  --drop = gravity/2 * t²

            yVelocity = (yVelocity + drop)
            local target = (origin * CFrame.new(0, 0, -self.Speed * step)) - Vector3.new(0, yVelocity, 0)
            local rayResult = workspace:Raycast(origin.Position, (target.Position - origin.Position).Unit * (self.Speed * step), raycastParams)

            if rayResult then
                self.Projectile.CFrame = CFrame.new(rayResult.Position, target.Position)
                if rayResult.Instance then
                    result = rayResult
                end
            else
                self.Projectile.CFrame = CFrame.new(target.Position, origin.Position) * CFrame.Angles(0, math.rad(180), 0)
            end

            origin = target
        end

        print("Projectile took", tick() -start, "seconds")

        if result then
            print(result)
            local surface = self:GetSurface(result)
            print(surface)
        end
    end

2 answers

Log in to vote
1
Answered by 3 years ago

I would personally use a BodyPosition with a touched event to handle this, you can probably also use the touched event with the tween service which is easier to work with. Raycasting is better in order to predict a target rather than for collisions

0
I don't think the touched event fires when the projectile is anchored, also a BodyPosition is not good for this at all Valenthian 130 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Your script doesn’t work because the function is not local.

Answer this question