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

Question How to make a trajectory prediction path take gravity and max speed into account?

Discussion in '2D' started by sanstheboss74, Oct 20, 2023.

  1. sanstheboss74

    sanstheboss74

    Joined:
    Jan 7, 2023
    Posts:
    1
    Hello! I am trying to make a game where the mouse is dragged across the screen to launch an object (For Example: Angry Birds), and I have a Trajectory system I wish to add two details into but I'm struggling to find a way to do so.

    I have a max speed cap which, by itself, does limit the speed of the object, but I wish for the trajectory line to slow and stop the more it progresses towards the max speed to make it more accurate.

    I also have a mechanic which switches the balls gravity upon passing a trigger and I want the trajectory line to curve with the gravity to show where the ball will end up after switching gravity.

    This is part of my GameManager script:

    Code (CSharp):
    1. void OnDragStart()
    2.     {
    3.         ball.DeactivateRb();
    4.         startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    5.         trajectory.Show();
    6.     }
    7.  
    8.     void OnDrag()
    9.     {
    10.         endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    11.         distance = Vector2.Distance(startPoint, endPoint);
    12.         direction = (startPoint - endPoint).normalized;
    13.         force = direction * distance * pushForce;
    14.         Debug.DrawLine(startPoint, endPoint);
    15.         trajectory.UpdateDots(ball.pos, force);
    16.     }
    17.  
    18.     void OnDragEnd()
    19.     {
    20.         ball.ActivateRb();
    21.         ball.Push(force);
    22.         trajectory.Hide();
    23.         if (power >= 1)
    24.         {
    25.             power--;
    26.         }
    27.     }
    This is part of my Trajectory script:

    Code (CSharp):
    1.     void PrepareDots()
    2.     {
    3.         dotsList = new Transform[dotsNumber];
    4.         dotPrefab.transform.localScale = Vector3.one * dotMaxScale;
    5.         float scale = dotMaxScale;
    6.         float scaleFactor = scale / dotsNumber;
    7.         for (int i=0; i<dotsNumber; i++)
    8.         {
    9.             dotsList[i] = Instantiate(dotPrefab, null).transform;
    10.             dotsList[i].parent = dotsParent.transform;
    11.             dotsList[i].localScale = Vector3.one * scale;
    12.             if (scale > dotMinScale)
    13.             {
    14.                 scale -= scaleFactor;
    15.             }
    16.         }
    17.     }
    18.  
    19.     public void UpdateDots(Vector3 ballPos, Vector2 forceApplied)
    20.     {
    21.         timeStamp = dotSpacing;
    22.         for(int i = 0; i<dotsNumber; i++)
    23.         {
    24.             pos.x = (ballPos.x + forceApplied.x * timeStamp);
    25.             pos.y = (ballPos.y + forceApplied.y * timeStamp) - (Physics2D.gravity.magnitude * timeStamp * timeStamp) / 2f;
    26.             dotsList[i].position = pos;
    27.             timeStamp += dotSpacing;
    28.         }
    29.     }
    And this is part of the script for the actual object itself:

    Code (CSharp):
    1.    public void Push(Vector2 force)
    2.     {
    3.         rb.AddForce(force, ForceMode2D.Impulse);
    4.     }
    5.  
    6.     public void ActivateRb()
    7.     {
    8.         rb.isKinematic = false;
    9.         rb.velocity = Vector3.zero;
    10.         rb.gravityScale = gm.grav;
    11.     }
    12.  
    13.     public void DeactivateRb()
    14.     {
    15.         rb.velocity = Vector3.zero;
    16.         rb.angularVelocity = 0f;
    17.         rb.isKinematic = true;
    18.     }
    I hope that i've been clear and understandable enough, if there's anything else I need to add please let me know, if anyone can help me out with this that would be very much appreciated

    It's worthy to note that I have not written all of this code by myself, and have only made edits to it to add my own features.

    Original Source: (3467) Unity 2D trajectory predict tutorial , angry birds [Part 2] - YouTube