2d Arrow shooting to a moving target.

Hi,

I am trying to bring

the arrow shooting effect as shown in this video. I dont want the exact physics for the arrow i just want to make it look like its following the trajectory and hitting the target. I can achieve the rotation of the arrow with the trajectory by using this code

var dir = rigidbody2D.velocity;
if (dir != Vector3.zero) {
     angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}

What i want to achieve is that the arrow should hit the target any how and should get the height and follow a trajectory. Please refer to video at 1:00. the archers at the back of castle.

1 Like

You’re smart not to bring physics into this! That frees you to solve a problem in any way that’s convenient.

If you’re reasonably good at high school algebra, you could do this “properly” by noting that an object in free fall (and ignoring drag) follows a parabola. You know the two endpoints (where the arrow begins, and where it wants to end), so you can define a formula for it as explained here. Basically if you want the arrow to start at x0 and end at x1, then its Y position for any x will be startY + k * (x - x0) * (x - x1). By changing the constant x, you can control how high the arrows arc.

That’s assuming, of course, that the arrows end at roughly the same Y as where they start. If not, you can still find a parabola, but it’s a little more involved (you’d have to use solution 1 rather than solution 2 in the link above).

(Hey kids! If anybody out there is asking your math teacher, “Why would I ever need to know this stuff?”, this is why!)

To make the arrow point in the direction it’s going, you’d just use your equation for Y with a slightly different X. This gives you a slightly different Y, and then you can have your arrow look at that point, using Atan2 like you’re doing now. (Or if you’re comfy with calculus, you could take the derivative of your parabola equation and use that… but the simple difference approach I just described will be plenty good enough.)

If all this algebra has got you stumped, I’m sure we can come up with other ways to do it… there are a zillion ways to skin this particular cat. Post back and let us know how it goes!

3 Likes

For posterity, I wrote up this approach here. So if anybody wants more details, please check it out!

2 Likes

Hi @JoeStrout , Thanks for the Answer, i didn’t got the notification for the answer. Seems like it will work for my need. I will Definitely try your answer.

1 Like

One other thing you might need to do. Obviously you are going to have to change his targetPosition from a public Editor value to one you can pass in. The other thing you might need to do when you target an enemy is figure out where he is based on how long you want your arrow animation to last. Lets say 2 seconds… You will then need to get the enemy’s Transform position and figure out where it will be 2 seconds from now, based on its speed. I assume you’ll have some fast moving enemies and some slower ones. This will your arrow will end its movmenent where the enemy will be, not where it is now.

Here’s Joe Strout Blog about Arching Projectiles in Unity! Hope this helps and saves you time! =)
Link: Luminary Apps : Blog

5 Likes

I’ve been looking for a simple arc solution such as this for a while now. Thank you so much.

Edit: when the target X coordinate is very close, the arrow shoots too fast. Is there any way to slow it down, maybe make it always take a second to reach the target?

Edit2: Ok, I think I figured it out. I changed the line:

float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);

to lerp:

float nextX = Mathf.Lerp(startPos.x, x1, passedTime / totalTime);

Just declare float passedTime and add this on Update:
passedTime += Time.deltaTime;
totalTime is the overall time you want the arrow to reach its target. Do notice that with this solution, the arrow will always take the same amount of time to reach its target no matter how close or far it is. But this is better behavior for tower defense game as you want to create an illusion the arrow is shooting upwards and taking a short while even if the enemy is very close to the tower.