Help with calculating 'Grenade' projectile trajectories

I’ve gotten myself in a bit deep with Unity already, and I’m only in the second semester of a Computer Games Design course. Because of that, to continue with my idea, I need a hand from someone who’s experienced with coding in Unity and also a dab hand at maths.

One of the specific points of the brief in this topic is that there must be some form of projectile existing within the game’s structure to demonstrate the ability to use rigidbody components. While this is a simple enough thing to implement in, say, a FPS styled game where you would only have to click a button and the physics engine would do the work. I’ve decided - with the lecturer’s permission - to bend the rules a bit, and am doing a isometric styled RTS/RTT game al la World in Conflict - which causes some issues with projectiles. While I can simply code in pressing a button and launching a projectile, I need to be able to click a location, and the game calculate the power needed to throw the grenade at a 45 degree angle.

I know I need the angle, power and target of the grenade in order to create the formulae to do this. I’ve got the angle - 45 degrees - and I’ve got the target - the point in which a raycast from the mouse location intersects the terrain. My problem is how to get the relation between the target and the angle to determine the power required to get the location. Does anyone know the formulae required for this, and how I’d write a script to implement it?

If you aren’t changing the angle, especially using 45 degrees, it’s a lot easier.

To make it simpler, find the forward/up speed (at 45, they are the same,) instead of the total speed. cosine of 45 of 0.707, so you can divide by that to get total speed needed (if you need 10 forward speed, use 10/0.707 total speed at 45 degrees.)

The ball will be in the air for exactly speed/9.8*2 seconds. To see this, suppose you shoot it with up=19.7. It will go up for 2 seconds until gravity kills up speed, then the exact reverse process take 2 seconds to fall (and it will be falling down at the exact speed you fired it up.) If the target isn’t on the same level, there are formulas for the time.

The distance is (note that forward speed = up speed): speedtime == speed * speed/4.9. Solving for speed, you get: SS=D4.9, or S=(sqrt(D4.9).)

Here’s a link to a UnityAnswers post asking questions about something similar. There is some code halfway down the page for calculating a firing solution, but I’d spend some time both understanding the math and the code before I’d use it:

http://answers.unity3d.com/questions/49195/trajectory-of-a-projectile-formula-does-anyone-kno.html

Here are some links to the math:

http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html#tra14

http://en.wikipedia.org/wiki/Trajectory