Alright. So I got a bunch done today.
I added Bazooka shooting animations, added enemy machine gun animations, updated this website, created a combat scene, cut apart the explosion animations, created a new animation library for explosions, created a new sprite library for explosions, rockets, grenades, then I programmed the grenade object, the explosion object, the code to throw a grenade…
It’s been a long day.
But what got me caught up for an hour or two was getting the fake 3D, 2D grenade parabola correct.
It started out simple. 2 second throw, so take the start, take the end, divide by two, use that speed as the translation. Great if you want to throw the grenade at the enemy like a fast ball.
Now, add a gravity like parabola? On TOP of that linear motion, because in 2.5D that 2 dimensional move is just supposed to be the X…?
Well, I added a sine curve on top of the position each frame, but then that made the grenade just fly off into space. Ah, adding every frame is taking the integral of the sine curve, which of course keeps growing. So we need to add the difference. But oh, hey, just in case it wasn’t confusing enough, the standard sin function is in radians, so make sure you remember your degrees to radians conversions. And then remember you want the sine to divide by two because of the 2 second flight time. And don’t forget that you want this to be frame dependent speed, so make sure to play with Time.deltaTime… Anyways, at one point the line of code was like this:
transform.position.y += (Time.deltaTime * 10 * Mathf.Sin((Time.time – throwStartTime)/2)*Mathf.PI) – (Time.deltaTime * 10 * Mathf.Sin((Time.time – throwStartTime + Time.deltaTime)/2)*Mathf.PI);
See what I did there? I hope not. If so, please go make your own game. Or work for me cheap.
Anyways, finally got it working after beating my (admittedly non-engineery) head against a wall for a while. Here’s final code:
var arc : float;
//Flat travel variables
transform.position.x += Time.deltaTime * xspeed;
transform.position.y += Time.deltaTime * yspeed;
//Parabolic arc for Y
arc = 5*Mathf.Sin((Time.time – throwStartTime)/2*Mathf.PI) – prevYHeight;
transform.position.y += arc;
prevYHeight = 5*Mathf.Sin((Time.time – throwStartTime)/2*Mathf.PI);