I have a game that draw path before throwing arrow but path doesn't fit with path of arrow after throwing.
this is the code of path:
public void DrawThePath()
{
cm.clear_circles();// cm is object for drawing circle
float x = transform.position.x;
float y;
for (int i=0; i<circleNumbers; i++)
{
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
x += circleXdistance;
if (x > Aim.transform.position.x)
{
break;
}
}
x = Aim.transform.position.x;
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
}
private float calculateHeight(float x)
{
float g = Physics2D.gravity.y;
float theta = Arrow.transform.rotation.z * 2 / 3 * Mathf.PI;
float velocity = FindObjectOfType<HandMove>().getVelocity();
float initialY = Arrow.transform.position.y;
float y = g / 2 * Mathf.Pow(x / (Mathf.Cos(theta) * velocity), 2);
y += Mathf.Tan(theta) * x;
y += initialY;
return y;
}
and the code of throwing arrow:
public void throwArrow()
{
velocity = FindObjectOfType<HandMove>().getVelocity();
GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
GetComponent<Rigidbody2D>().velocity = new Vector2(velocity * Mathf.Cos(Angle), velocity * Mathf.Sin(Angle));
}
That cannot work simply because the actual arrow works using physics while you are just drawing an arc. It's not that simple I'm afraid.
A very good predictive working example (not a trajectory plotting), with Unity project demo as well, is presented here: https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/ it can give you a good background on how physics works.
There is also an old but still, valid code that solves your specific request on Unity Wiki: http://wiki.unity3d.com/index.php?title=Trajectory_Simulation&_ga=2.144123763.792205966.1600849394-1294758123.1579039644
~Pino