title: 2D Projectile Trajectory Prediction(unity 2D)
body: <p>using (unity 2019.3.7f1) 2d.
  I have a player that moves around using a pullback mechanic and has a max power(like in Angry Birds).
  I'm trying to draw a line(using a line renderer) that shows the exact path the player will go. I'm trying to make the line curve just like the player's path will. so far I've only managed to make a straight line in a pretty scuffed way.
  The known variables are the Jump Power and the player's position, there is no friction. and I believe gravity is a constant(-9.81). Also, I would like to have a variable that allows me to control the line's length. And, if possible, the line will not go through objects and would act as if it has a collider. </p>
  
  <p>// Edit</p>
  
  <p>This is my current code. I changed The function so it would return the list's points because I wanted to be able to access it in Update() so it would only draw while I hold my mouse button.</p>
  
  <p>My problem is that the trajectory line doesn't seem to curve, it goes in the right angle but it's straight. the line draws in the right direction and angle, but my initial issue of the line not curving remains unchanged. If you could please come back to me with an answer I would appreciate it. </p>
  
  <pre><code>enter code here
  
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class TrajectoryShower : MonoBehaviour
  {
  LineRenderer lr;
  public int Points;
  
  
  public GameObject Player;
  
  private float collisionCheckRadius = 0.1f;
  
  public float TimeOfSimulation;
  
  private void Awake()
  {
      lr = GetComponent&lt;LineRenderer&gt;();
      lr.startColor = Color.white;
  }
  
  // Update is called once per frame
  void Update()
  {
      if (Input.GetButton("Fire1"))
      {
          lr.positionCount = SimulateArc().Count;
  
          for (int a = 0; a &lt; lr.positionCount;a++)
          {
              lr.SetPosition(a, SimulateArc()[a]);
          }
      }
      if (Input.GetButtonUp("Fire1"))
      {
          lr.positionCount = 0;
      }
  }
  
  private List&lt;Vector2&gt; SimulateArc()
  {
      float simulateForDuration = TimeOfSimulation;
      float simulationStep = 0.1f;//Will add a point every 0.1 secs.
  
      int steps = (int)(simulateForDuration / simulationStep);
      List&lt;Vector2&gt; lineRendererPoints = new List&lt;Vector2&gt;();
      Vector2 calculatedPosition;
      Vector2 directionVector = Player.GetComponent&lt;DragAndShoot&gt;().Direction;// The direction it should go
      Vector2 launchPosition = transform.position;//Position where you launch from
      float launchSpeed = 5f;//The initial power applied on the player
  
      for (int i = 0; i &lt; steps; ++i)
      {
          calculatedPosition = launchPosition + (directionVector * ( launchSpeed * i * simulationStep));
          //Calculate gravity
          calculatedPosition.y += Physics2D.gravity.y * (i * simulationStep);
          lineRendererPoints.Add(calculatedPosition);
          if (CheckForCollision(calculatedPosition))//if you hit something
          {
              break;//stop adding positions
          }
  
      }
  
      return lineRendererPoints;
  
  
  
  }
  private bool CheckForCollision(Vector2 position)
  {
      Collider2D[] hits = Physics2D.OverlapCircleAll(position, collisionCheckRadius);
      if (hits.Length &gt; 0)
      {
          for (int x = 0;x &lt; hits.Length;x++)
          {
              if (hits[x].tag != "Player" &amp;&amp; hits[x].tag != "Floor")
              {
                  return true;
              }
          }
  
      }
      return false;
  }
  </code></pre>
  
  <p>}</p>