What: I’m trying to make a Peggle style aiming system, for anyone not familiar here’s a youtube of Peggle (note the aiming system at the top of the screen): http://www.youtube.com/watch?feature=player_detailpage&v=ZMo2Vegh8hA#t=195s
It’s essentially a downward trajectory shot, taking gravity into account.
Background: I did a lot of searches on trajectory examples in Unity and here’s the issue I ran into. They all seem to assume you are firing along the horizontal or X axis (Angry Birds or Worms for example). Indeed most math I looked up to explain trajectories seems to assume you want to shoot to the left or right on the horizontal axis (whereas I want to always shoot down on the Y or vertical axis).
This results in shooting the ball UP before it falls, producing some awkward shots. I want it to shoot down but with some force and taking into account gravity (ala Peggle).
To help you help me, here’s what I’m working off of.
- I can grab the X,Y coordinate of the mouse using essentially this which is called during Update():
function GetXY()
{
ray = cannonCamera.ScreenPointToRay(Input.mousePosition); // create a ray from the mousePosition
if (plane.Raycast(ray, dist)) //dist is a float from the ray start to the hit point
{
aimPoint = ray.GetPoint(dist); //aimPoint is a Vector3 describing where the mouse is hovering
desiredShot = aimPoint - transform.position; //desiredShot is a Vector3 between our mouse cursor and the cannon making the shot
desiredShot.z = 0; //leaves us with just X,Y
}
}
-
I can return the theta or angle of the mouse cursor using the trig circle, which means when aiming down that I return an angle somewhere between 180 and 360 (which looks like this):
-
Just to give you a visual example of #1 and #2 in action, here is the game returning the X,Y position and Angle of the mouse position on screen:
Help: Can someone give me guidance on how I can accomplish this downward trajectory shot, what would be the appropriate equation? Am I thinking/approaching this wrong?
I originally tried trajectory solutions I found searching Unity Answers but they have the issue my above webplayer has (for example, Calculating ball trajectory in full 3d world - Questions & Answers - Unity Discussions). I tried using the math here, but to no avail: Non-Horizontally Launched Projectile Problems.
Any help would be deeply appreciated, I’ve tried for days here with no luck! Code samples are useful but even just pointing me towards the right formula would be extremely useful, it’s been 15 years since I’ve done any trig! Thanks!
Skeptika
Edit: I’m thinking about trying this: Trajectory of a projectile formula. Does anyone know maths? - Questions & Answers - Unity Discussions which is using this: Projectile motion - Wikipedia (go down to Angle to hit coordinate X,Y)… is this the approach I should be taking? If so, once I calculate the Theta required, how do I actually convert this into a fired shot (rotate object to appropriate Theta and fire transform.forward with assumed force)?