How do i predict the trajectory of my arrow?

Hey everyone,

I’m currently working on implementing an arrow firing mechanism in my Roblox game, and I’m looking for guidance on how to accurately predict the trajectory of the arrow.

Problem:

I’m unsure of how to predict the trajectory of my arrow which uses velocity when players release the mouse button to shoot. My goal is to predict where the arrow will go and visualize it for the player, reference:

I don’t really know how to make it so i just replaced it with a part

Client-Sided:


local mouse = game.Players.LocalPlayer:GetMouse()
local buttonDown = false
local arrowPreview = Instance.new("Part")
arrowPreview.Size = Vector3.new(0.2, 0.2, 10)
arrowPreview.Transparency = 0.5
arrowPreview.Material = Enum.Material.Neon
arrowPreview.Color = Color3.new(0, 1, 0)
arrowPreview.Anchored = true
arrowPreview.CanCollide = false
arrowPreview.Parent = game.ReplicatedStorage

mouse.Button1Down:Connect(function()
    buttonDown = true
    arrowPreview.Parent = workspace
end)

mouse.Button1Up:Connect(function()
    script.Parent.BowRE:FireServer("shoot", script.Parent.Bow.Middle.Position.Z, mouse.Hit)
    arrowPreview.Parent = game.ReplicatedStorage
    buttonDown = false
end)

game:GetService("RunService").RenderStepped:Connect(function()
    if buttonDown then
        script.Parent.Bow.Middle.Position = script.Parent.Bow.Middle.Position:Lerp(Vector3.new(0, 0, 1.998), 0.1)
    else    
        script.Parent.Bow.Middle.Position = script.Parent.Bow.Middle.Position:Lerp(Vector3.new(0, 0, 0.637), 0.7)
    end
    script.Parent.Bow.Weld.C1 = CFrame.new(-Vector3.new(0, 0, script.Parent.Bow.Middle.Position.Z - 0.7))
    local arrowStart = script.Parent.Arrow.Position
    local direction = (mouse.Hit.Position - arrowStart).unit
    local arrowEnd = arrowStart + direction * 10
    arrowPreview.Position = arrowStart
    arrowPreview.CFrame = CFrame.lookAt(arrowStart, arrowEnd)
end)

script.Parent.BowRE.OnClientEvent:Connect(function(arrow, charge)
    arrow:Destroy()
    local newArrow = script.Parent.Arrow:Clone()
    newArrow.Parent = workspace
    newArrow.Transparency = 0
    newArrow.CFrame = CFrame.new(newArrow.Position, mouse.Hit.Position)
    newArrow.Velocity = mouse.Hit.LookVector * charge * 200
    newArrow.Touched:Connect(function(hit)
        if hit.Parent ~= game.Players.LocalPlayer.Character and hit.Parent.Parent ~= game.Players.LocalPlayer.Character then
            newArrow:Destroy()
        end
    end)
end)

while wait(0.1) do
    script.Parent.BowRE:FireServer("string", script.Parent.Bow.Middle.Position, script.Parent.Bow.Weld.C1)
end

I can provide the server script if needed

1 Like

you can use physics operations for this
https://www.youtube.com/watch?v=ZZ39o1rAZWY
this video explains it