Need better and accurate projectile throwing

I wanna update my now outdated projectile throwing system since it’s very much unrealistic and could be unpredictable, how can I improve it?
MouseDir is the mouse position.

local BodyVelocity = Instance.new("BodyVelocity", Handle)
BodyVelocity.Velocity = (MouseDir.LookVector + Vector3.new(0, 0, 0)) * 90
Debris:AddItem(BodyVelocity, 0.50)

Note this script is very old (like a year old), hence the deprecated uses, like passing parent as second argument on instance creation and debris?

Modeling the path of your projectiles using projectile motion principles will help immensely for both realism and predictability.

Since air resistance is by default non-existent in ROBLOX and gravity is always an applied force, all you have to do is calculate the angle and force a projectile is thrown at, and it will, by nature of ROBLOX’s physics engine, be thrown in a consistent parabolic arc.

hence the deprecated uses, like passing parent as second argument on instance creation and debris?

Instance.new(Instance, Parent) is not deprecated, nor is Debris.

1 Like

Passing parent as second argument in instance new is heavily discouraged and debris runs on 30 hz from what’ve heard.

1 Like

Not deprecated as OP stated, but yes, using the second argument of Instance.new() is generally a bad idea.

I don’t think Debris running at 30 hertz (which I think is an artifact of ROBLOX’s 30Hz pipeline which is where the old wait, spawn, delay functions lived) is going to cause serious issues for OP based on their use case. It’s still useful for delayed destruction of items, but you could always use task.delay() to create delayed destruction instead

1 Like

Hey so I followed this tutorial Modeling a projectile's motion but the velocity was very fast and sometimes the handle would fly upwards when I click on my torso, how do I tone it down?

local GForce = Vector3.new(0, -workspace.Gravity, 0)
	local x0 =  Character.PrimaryPart.CFrame * Vector3.new(0,2,-2)
	local v0 = (Player:GetMouse().Hit.Position - x0 - 0.5*GForce)/2
	local Gas = Tool.Handle:Clone()
	Gas.Velocity = v0
	Gas.CFrame = CFrame.new(x0)
	Gas.Parent = workspace.__SPAWNEDSTUFF

I would tone down -workspace.Gravity, as it’s the part of your force responsible for pulling the projectile downwards. Perhaps divide it by a factor of 2 or 4?

Also, it’s flying upwards because that’d be like throwing a ball straight up in real life. You may need to use a different formula or method. Remember, all you need to do is calculate the starting angle and force at which to throw your projectile. ROBLOX’s physics engine can take care of the rest, unless you’re wanting to model the entire path of the projectile yourself.

What I’m trying to do is a simple grenade throw, how you would do it irl, but nobody would throw a grenade upwards also I’ll try your suggestion.

You forgot to put t to calculate the initial velocity. By varying t you can get different heights.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local t = 0.5
function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local GForce = Vector3.new(0, -workspace.Gravity, 0)
        local x0 =  Character.PrimaryPart.CFrame * Vector3.new(0,2,-2)
        local v0 = (Player:GetMouse().Hit.Position - x0 - 0.5*GForce*t*t)/t
        local Gas = Instance.new("Part")
        Gas.Size = Vector3.new(1,1,1)
        Gas.Velocity = v0
        Gas.CFrame = CFrame.new(x0)
        Gas.Parent = workspace
    end
end
game:GetService("UserInputService").InputBegan:Connect(onInputBegan)
1 Like

just to clarify, this is what the documentation says

When creating an object then setting many properties, it’s recommended to set Parent last

here’s why

This ensures the object replicates once, instead of replicating many property changes

So it is not recommended only for objects that are going to be replicated, for objects parented to Workspace or ReplicatedStorage for example. But if they are parent to ServerStorage or ServerScritpService there should be no problem.

However, if you were parenting your parts to a Model whose parent hasn’t been set yet, then setting the parent first would not matter as the model would not have replicated yet.

Sometimes it helps to save some lines, e.g.

Instance.new("Folder", ServerStorage).Name = "Configurations"
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.