- Home /
Question by
IemandDieAbeHeet · Aug 03, 2019 at 07:59 PM ·
physics2dtrajectoryarc
Rendering a 2D trajectory arc
Hello!
I've been trying to render a 2D trajectory arc with yellow prefabs for a while now but I keep failing. Is there anyone that has an idea on how to do this?
Thank you!
Comment
Answer by IemandDieAbeHeet · Aug 03, 2019 at 09:37 PM
Here is the code I currently have. The x and y formulas seem to be messed up
{
public GameObject indicator;
public float velocity;
public float angle;
public int resolution = 10;
private List<Vector2> trajectoryPoints;
private float g;
private void Start()
{
trajectoryPoints = new List<Vector2>();
g = Mathf.Abs(Physics2D.gravity.x);
}
private void Update()
{
trajectoryPoints.Add(new Vector2(1, 1));
CalculatePoints();
}
private void CalculatePoints()
{
float range = (2 * velocity) / g;
for(int i = 0; i <= resolution; i++)
{
float x = range * i;
float y = y = x * Mathf.Tan(angle) - g * x * x / 2 * velocity * Mathf.Cos(angle) * Mathf.Cos(angle);
trajectoryPoints.Add(new Vector2(x, y));
Debug.Log(trajectoryPoints[i]);
Instantiate(indicator, new Vector2(x, y), Quaternion.identity);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612210007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Aim preview for projectile trajectory 1 Answer
How do I arc a rigidbody so it lands on a target? 2 Answers
trajectory arc not working, how to build a trajectory arc for aiming 0 Answers
How to make an arrow fly realistically. 3 Answers
Triying to make a character take knockback in a parabolic motion in 2D. 0 Answers