#Gun Raycast bullet visualization

1 messages · Page 1 of 1 (latest)

remote onyx
#

Im currently learning about raycasting and i have this issue with the endpoint of the bullet raycast, as seen in the video when i shoot in the air the endposition of the bullet does not match the players cursor and i haven't found out how to fix it.

in the script below are my calculations to visualize the bullet:

#
local function applySpread(direction, spreadAmount)
    local spreadRadians = math.rad(spreadAmount)

    local spreadX = math.random() * spreadRadians - (spreadRadians / 2)
    local spreadY = math.random() * spreadRadians - (spreadRadians / 2)
    local spreadZ = math.random() * spreadRadians - (spreadRadians / 2)

    return (direction + Vector3.new(spreadX, spreadY, spreadZ)).Unit
end

local function visualizeBullet(muzzlePos, hitPoint, bullet)
    local direction = hitPoint - muzzlePos
    local length = direction.Magnitude

    local middlePoint = muzzlePos + direction * 0.5

    local bullet = Instance.new("Part")

    bullet.Size = Vector3.new(0.15, 0.15, length)
    bullet.CFrame = CFrame.new(middlePoint, muzzlePos + direction)

    tweenService:Create(bullet, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Transparency = 1}):Play()

    game.Debris:AddItem(bullet, 0.5)

    table.insert(excludedObjects, bullet)
end

fireEvent.OnServerEvent:Connect(function(player, muzzlePos, mousePos, spreadAmount)
    table.insert(excludedObjects, player.Character)

    local character = player.Character or player.CharacterAdded:Wait()
    local head = character:FindFirstChild("Head")

    local baseDirection = (mousePos - head.Position).Unit * 50
    local spreadDirection = applySpread(baseDirection, 70) * 50 

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = excludedObjects
    raycastParams.FilterType = Enum.RaycastFilterType.Exclude

    local raycastResult = workspace:Raycast(head.Position, spreadDirection, raycastParams)

    local hitPoint    
    if raycastResult then
        hitPoint = raycastResult.Position
    else
        hitPoint = head.Position + spreadDirection
    end

    visualizeBullet(muzzlePos, hitPoint)
end)
#

i have left a few unnecessary parts out

fickle heath
#

I think its because you use a different start position for your ray cast and your visualize function. The part should in theory still expand to your hitpoint. But because your hitpoint is infinitly far away the difference beetwen how the ray actually "travels" and how the visulize part looks like is way bigger.

#

This is just a theory but i am strongly believing that i am right.

fickle heath
eternal ravine
#

probably because of the spread

#

did you write the code?

#

or copy from a tutorial?

remote onyx
remote onyx
fickle heath
#

as the rays path

remote onyx
#

because you that the hitpoint is infinitely far away, is it possible to just reduct it to its max range something like that

fickle heath
#

the ray will travel as long as it doesn't hit something

fickle heath
remote onyx
remote onyx
#

1 second

fickle heath
remote onyx
#

this is the same as in the game ive shown

#

there's no other way to replicate this

#

i just want to find out how to calculate to fix this infinitely far away hitpoint as you've mentioned

fickle heath
#

But no that would still use the old direction of your infinitly far away hitpoint nvm

#

Maybe try creating a part at your max distance before shooting your ray so that your ray finds that part and your hitpoint isn't infinitly far away

#

Anyways I gotta go now tell me once you've got it or if it there is another problem

remote onyx
#

yeah ill try