Throwing things... could use some help with calculating forces.

So, i’ve got the raycast working to choose a point where i want to throw to and i’ve been able to get the prefabs instantiating properly… but i can’t for the life of me figure out how much force to add so that when it’s thrown at an angle how to get it to where i need it to.

at first i plotted the forces applied and the distance from the player (he’s throwing a grenade just in case you were wondering and the grenade has a rigidbody so that i can have it affected by gravity) and tried to calculate a basic force multiplier which worked somewhat for smaller distances (at the angle i was throwing it, i managed to figure out for close range, multiplying it by a factor of 40 got me hitting the target roughly… but at long range he really throws it a long distance, way too far.

i tried googling calculating balistics but the maths was WAY over my head. i’m talking “brain showing static on the inner viewscreen” over my head. nevermind how i’m not even sure how to execute this in code at all.

So, have you made a character throwing something and have you made it such that it hits where you want them to throw it to? would you please help a fellow out by assisting with what i need to do to get this going accurately?

Bump.

I know i could just throw the grenade with a fixed amount of force but i’m hoping to have a mechanism set up so that you can target where you want to throw it… if using forces is the wrong way to go about it, please let me know… i’ve thought that maybe if i could get some higher trig functions set up, that might do it but my coding skills (and math skills) aren’t all that great.

basically, the way i want it to work is as such… when you click the mouse, a reticle appears where your mouse is that has a relatively large radius… the longer you hold it, the smaller this reticle becomes… and when you release the button, it’ll pick a random point within that reticle to throw the grenade… and the clincher is that if you hold it too long, the grenade will detonate in your hand causing damage to the player… so, it’s a balance between whether you want to work for perfect timing skills (the window between a perfect throw and the detonation is less than a second) and risk damage or if you want to just lob the grenade willy-nilly and not risk taking damage.

so, if you want to toss a lot of grenades and you don’t have a problem if they don’t hit where you want exactly, you can… but if you want precision, you have to hold the mouse button down.

You don’t need to calculate the force, actually. Since the only impulse is the initial throw, simply setting the rigidbody’s velocity vector will work just as well, and is a bit easier to calculate. The range equations for parabolic trajectory can be found in any text. The following equation assumes that the terrain is flat.

R = v^2 * sin(2*θ) / g

Where:
R = Target range (meters)
v = Initial velocity (m/s)
θ = Initial angle (radians)
g = Positive acceleration due to gravity (m/s^2)

Solve for velocity…

v = √(R * g / sin(2*θ))

using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour 
{
	public float Range = 20;
	public float Angle = 65;
	public GameObject Target = null;
	
	void Start () 
	{
		// Calculate trajectory velocity
		Vector3 v = new Vector3(0, Mathf.Sin(Angle * Mathf.Deg2Rad), Mathf.Cos(Angle * Mathf.Deg2Rad));
		rigidbody.velocity = v * Mathf.Sqrt(Range * Physics.gravity.magnitude / Mathf.Sin(2*Angle*Mathf.Deg2Rad));
		
		// Position another object at the target range to verify that the trajectory calculate works.
		if (Target != null)
			Target.transform.position = transform.position + transform.forward * Range;
	}
}

I could kiss you, Brian! i was hoping for some help calculating the force neccessary but you went above and beyond!

many thanks!

This dows only shoot the object along the x axis. How could i shoot it along its local look forward rotation?

Regards,
Markus