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

How to limit how far a projectile can travel and if that limit is reached, make it go that far?

Asked by 4 years ago

(Reposted because never answered) Hello! I am trying to make a throwing system and I want my throwing system to allow the projectile to go for example 50 studs maximum. I slightly modified @EgoMoose's Modeling projectile motion script which can be found Here. As it stands, my current code is :

local t = 1;
local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart");
local bball = script.Parent

mouse.Button1Down:Connect(function()
    if script.Parent.Parent.Parent ~= workspace then
        local g = Vector3.new(0, -game.Workspace.Gravity, 0);
        local x0 = hrp.CFrame * Vector3.new(0, 2, -2)

        local pos = mouse.Hit.p
        if (hrp.Position - pos).magnitude > 50 then --this is to make sure it does not travel more than 50 studs
        --im guessing the code to make it go 50 would go here.
        else
            local v0 = (pos - x0 - 0.5*g*t*t)/t;
            local c = bball:Clone();
            c.Velocity = v0;
            c.CFrame = CFrame.new(x0);
            c.CanCollide = true;
            c.Parent = game.Workspace;
        end
    end
end)

This limits the distance, however I don't know how to make it just go that distance. Any help is appreciated. Thanks, BrainDead_Dev.

Answer this question