I am working on a throwable grenade for a fighting game I am working on, and I was curious how I could make my projectile more accurate.
Currently it uses the mouses lookvector to control the velocity of a bodyvelocity
It works although it is very inaccurate, with it being basically luck on whether or not it will roll where you want it to, making it harder to hit targets effectively. How would I make it more accurate? I’m going for something like this:
I read through the post and couldn’t figure it out, I want the throw direction to be based off the player camera but the main thing I want is an arc type throw and have the grenades velocity accurate to where the mouse is positioned (meaning the grenade would stop where the you clicked with the mouse).
It works now, but it still isn’t what I am looking for, and when thrown close to yourself it will just launch in a random direction it seems. Roblox has a grenade tool in the toolbox that almost does what I am looking for (with angular throws and stuff) but the only problem with it is it always throws at an angle, meaning when thrown close to yourself it just arcs super high in the air like this:
This is the part of the roblox grenade code that does the angle stuff (I think):
-- Uber l33t maths to calcluate the angle needed to throw a projectile a distance, given the altitude of the end point and the projectile's velocity
function AngleOfReach(distance, altitude, velocity)
local gravity = workspace.Gravity
local theta = math.atan((velocity^2 + math.sqrt(velocity^4 -gravity*(gravity*distance^2 + 2*altitude*velocity^2)))/(gravity*distance))
if theta ~= theta then
theta = math.pi/4
end
return(theta)
end
local handlePos = Vector3.new(tool.Handle.Position.X, 0, tool.Handle.Position.Z) -- remove Y from the equation, it's not needed
local mousePos = Vector3.new(mousePosition.X, 0, mousePosition.Z) -- ditto
local distance = (handlePos - mousePos).magnitude -- Get the distance between the handle and the mouse
local altitude = mousePosition.Y - tool.Handle.Position.Y
local angle = AngleOfReach(distance, altitude, config.GrenadeVelocity.Value) -- Calculate the angle