Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2d Projectile Issues with getting projectile prediction after collision

Discussion in '2D' started by ani2020-bhandari, Jun 15, 2022.

  1. ani2020-bhandari

    ani2020-bhandari

    Joined:
    Aug 22, 2012
    Posts:
    5
    I have this code which simulates a projectile motion, it works well till I am just looking for prediction from just start position but when I try to Calculate the path after collision, I seem to have an issue, it will be great if someone can check my code and see what I am doing wrong here,
    The method OnThrow is called in Update() where I just pass in a startTransform.

    Code (CSharp):
    1.  private void OnThrow(Transform t)
    2.     {
    3.         lr.positionCount = 0;
    4.  
    5.         List<Vector3> positions = new List<Vector3>();
    6.  
    7.         InitialPos = t.transform.position;
    8.         float angle = m_Angle;
    9.  
    10.         for (float i = 0; i < 3; i += Time.fixedDeltaTime)
    11.         {
    12.             float x = InitialPos.x + m_Force * Mathf.Cos(angle * Mathf.Deg2Rad) * i;
    13.             float y = InitialPos.y + m_Force * Mathf.Sin(angle * Mathf.Deg2Rad) * i + 0.5f * -9.8f * i * i;
    14.             Vector2 pos = new Vector2(x, y);
    15.             positions.Add(pos);
    16.  
    17.             int count = Physics2D.OverlapCircleNonAlloc(pos, 0.1f, colliders);
    18.  
    19.             if (count > 0)
    20.             {
    21.                 int length = positions.Count > 2 ? positions.Count - 2 : positions.Count - 1;
    22.                 Vector2 dir = (pos - (Vector2)(positions[length])).normalized;
    23.                 RaycastHit2D hit = (Physics2D.Raycast(pos, dir, 0.1f)) ;
    24.  
    25.                 if(hit.collider!= null)
    26.                 {
    27.                     Vector2 reflect = Vector2.Reflect(dir, hit.normal);
    28.                     Debug.DrawRay(hit.point, dir, Color.black, 1);
    29.                     Debug.DrawRay(hit.point, reflect, Color.red, 1);
    30.                     float newAngle = Mathf.Atan2(reflect.y, reflect.x) * Mathf.Rad2Deg;
    31.                     angle = newAngle;
    32.                     positions.Add(hit.point + reflect * 1f);
    33.                     InitialPos = hit.point + reflect * 1f;
    34.                 }
    35.             }
    36.         }
    37.  
    38.         lr.positionCount = positions.Count;
    39.         lr.SetPositions(positions.ToArray());
    40.     }
    It should be following the red line but I am doing something wrong.
    Thanks for reading.
     
  2. ani2020-bhandari

    ani2020-bhandari

    Joined:
    Aug 22, 2012
    Posts:
    5
    Figured it out, naturally... after posting, realised, I wasn't resetting the time.
    i = 0; or and reduce the count, and it is golden
     
  3. ani2020-bhandari

    ani2020-bhandari

    Joined:
    Aug 22, 2012
    Posts:
    5
    Attaching the code,just in case someone comes looking for it.
    Code (CSharp):
    1.   private void OnThrow(Transform t)
    2.     {
    3.         lr.positionCount = 0;
    4.  
    5.         List<Vector3> positions = new List<Vector3>();
    6.  
    7.         InitialPos = t.transform.position;
    8.  
    9.         float angle = m_Angle;
    10.  
    11.         float timeToLoop = secondsToPredict;
    12.  
    13.         for (float i = 0; i < timeToLoop; i += Time.fixedDeltaTime)
    14.         {
    15.             float x = InitialPos.x + m_Force * Mathf.Cos(angle * Mathf.Deg2Rad) * i;
    16.             float y = InitialPos.y + m_Force * Mathf.Sin(angle * Mathf.Deg2Rad) * i + 0.5f * -9.8f * i * i;
    17.             Vector2 pos = new Vector2(x, y);
    18.             positions.Add(pos);
    19.  
    20.             int count = Physics2D.OverlapCircleNonAlloc(pos, 0.1f, colliders);
    21.  
    22.             if (count > 0)
    23.             {
    24.                 int length = positions.Count > 2 ? positions.Count - 2 : positions.Count - 1;
    25.                 Vector2 dir = (pos - (Vector2)(positions[length])).normalized;
    26.                 RaycastHit2D hit = (Physics2D.Raycast(pos, dir, 0.1f)) ;
    27.  
    28.                 if(hit.collider!= null)
    29.                 {
    30.                     Vector2 reflect = Vector2.Reflect(dir, hit.normal);
    31.                     Debug.DrawRay(hit.point, dir, Color.black, 1);
    32.                     Debug.DrawRay(hit.point, reflect, Color.red, 1);
    33.                     float newAngle = Mathf.Atan2(reflect.y, reflect.x) * Mathf.Rad2Deg;
    34.                     angle = newAngle;
    35.                     positions.Add(hit.point + reflect * 1f);
    36.                     InitialPos = hit.point + reflect * 1f;
    37.  
    38.                     timeToLoop -= i;
    39.  
    40.                     i = 0;
    41.                 }
    42.             }
    43.         }
    44.  
    45.         lr.positionCount = positions.Count;
    46.         lr.SetPositions(positions.ToArray());
    47.     }