How to make grenade projectile more accurate to the mouse?

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

image

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:

2 Likes

Its about perspective.
if u go in first person u will see that the grenade is actually accurate

if u are going to make your game third person then u can find probably your answer here How to make My Projectile more accurate with the mouse? - #6 by InfinityDesign

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).

I tried to do some similar code to what you mentioned but it didn’t really change anything, and in fact made the throw backwards.

bv.Velocity = (grenade.Position - mousePosition.LookVector).Unit * speed

change the speed value to be negative
bv.Velocity = (grenade.Position - mousePosition.LookVector).Unit * -speed

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:

https://gyazo.com/eb5a0283dcee7063bbf60258156384f6

I am looking for a mix between the two, so the further it gets thrown, the stronger the arc.

Give me sometime to explore the roblox grenade mechanics

i will see exactly what u are looking for

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
1 Like

i think i got it

local Pos = Mouse.Position
local MaxHeight = 30
local Height = math.clamp(Pos.Y,0,MaxHeight)

local Velocity = Vector3.new(Pos.X, Height Pos.Z) - Char.HumanoidRootPart.Position).unit *  (Vector3.new(Pos.X, Height Pos.Z) - Char.HumanoidRootPart.Position).magnitude

It says in the script for the velocity line incomplete statement, expected assignment or function call.

my bad now it should works

local Pos = Mouse.Position

local MaxHeight = 30

local Height = math.clamp(Pos.Y,0,MaxHeight)

local Velocity = (Vector3.new(Pos.X, Height, Pos.Z) - Char.HumanoidRootPart.Position).unit * (Vector3.new(Pos.X, Height, Pos.Z) - Char.HumanoidRootPart.Position).magnitude

2 Likes

This is great! I should be able to work off of this, thank you for your help!