- Home /
Creating a 2d arrow projectile in an arc that always hits its moving target
I have been looking for this for over a day and I can't find it.
I am trying to create an arrow thats instantiated from its tower and starts with a target (an enemy). The enemy is sometimes moving so it needs to hit it every time. I need the arrow to arch over before hitting the enemy.
This is what I have and it moves in a straight line, (and back after but that doesn't matter as it will destroy on impact) without any physics. It gets ward if I add any gravity.
ovrDistanceToTarget = startPos.x - target.transform.position.x;
currentDistanceToTarget = transform.position.x - target.transform.position.x;
Vector2 v = new Vector2 (target.transform.position.x - transform.position.x, target.transform.position.y - transform.position.y);
print (GetComponent<Rigidbody2D>().velocity);
float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
GetComponent<Rigidbody2D>().AddForce(transform.right * 15 * Time.deltaTime, ForceMode2D.Impulse);
Is there a better way to do this? Preferably without much physics.
Answer by maximeb-valtech · Sep 05, 2017 at 08:21 PM
If there is gravity, there will be physics. Wikipedia has a great article on projectile trajectory. I also happen to have a geogebra file will all the maths in a easy to understand form.
Answer by Raf2018 · Sep 06, 2017 at 12:37 AM
You could make it hit close enough by calculating the trajectory with physics and do damage or kill whoever triggered the shot, regardless if it hit or not. Or maybe you can instantiate a new object that does damage per area. Can't help much with code.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134246im_/https://answers.unity.com/themes/thub/images/avi.jpg)