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

Raycasts in while loop acting strangely?

Asked by 2 years ago
Edited 2 years ago

i'm trying to simulate bullets with multiple raycasts each at the end of the other, but the raycasts just completely separate from each other and fly off to the left?

i'm pretty sure it has something to do with the code that finds the ray origin but i'm not completely certain

what it should be doing: https://imgur.com/nlQihRX

what it's doing: https://imgur.com/QAumKQO

here's the entire (serverside) section of code related to the raycasts, with the exception of cameraDir which is just the client's camera cframe from a local script.

local function fire(cameraDir)
    local castDir = cameraDir.LookVector
    local bulletActive = true
    local currentBulletSpeed = bulletSpeed/bulletPathResolution
    local rayOrigin = Vector3.new(tool.MeshPart.Position.X, tool.MeshPart.Position.Y, tool.MeshPart.Position.Z)
    local dropSpeed = 0
    while bulletActive == true do
        local rayParams = RaycastParams.new()
        rayParams.FilterDescendantsInstances = {script.parent}
        rayParams.FilterType = Enum.RaycastFilterType.Blacklist
        local rayParam2 = castDir*(math.sqrt((currentBulletSpeed^2)+(dropSpeed^2)))
        local rayResult = workspace:Raycast(rayOrigin, rayParam2, rayParams)
        print("cast number "..(dropSpeed/(35.025/bulletPathResolution)+1).." going "..currentBulletSpeed.." st/s and falling at "..dropSpeed.." st/s from "..rayOrigin.X..", "..rayOrigin.Y..", "..rayOrigin.Z)
        local rayPoint = Instance.new("Part", game.Workspace)
        rayPoint.CanCollide = false
        rayPoint.Name = "raypoint"..(dropSpeed/(35.025/bulletPathResolution)+1)
        rayPoint.Anchored = true
        rayPoint.Size = Vector3.new(1, 1, (math.sqrt((currentBulletSpeed)^2+(dropSpeed)^2)))
        rayPoint.CFrame = cameraDir.Rotation
        rayPoint.Position = rayOrigin+castDir*((math.sqrt((currentBulletSpeed^2)+(dropSpeed^2)))/2)
        dropSpeed = dropSpeed+(35.025/bulletPathResolution)
        currentBulletSpeed = currentBulletSpeed/(1+(.001/bulletPathResolution))
        rayOrigin = rayOrigin+rayParam2
        castDir = Vector3.new(castDir.X-90+math.atan((currentBulletSpeed)/(dropSpeed)), castDir.Y, castDir.Z)
        wait(1/bulletPathResolution)
    end
    recoilAnim:Play()
end
0
this is being tested in roblox studio's "play" mode btw TheDude646 72 — 2y

Answer this question