Search Unity

predict trajectory 2D

Discussion in 'Physics' started by iboshido, May 11, 2019.

  1. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    Hey guys,

    i have a gameobject "ball", which i can fire out of a cannon. I copied a code to predict and show the trajectory of the ball. It worked perfectly until i added some lines of code to make the ball feel more real and less floaty. The trajectory prediction doenst show the real trajectory nomore.

    Code to predict trajectory of the ball:
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.  
    5. for (int k = 0; k < numberOfDots; k++)
    6. {                        
    7.    x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);  
    8.    y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (dotSeparation * k + dotShift));  
    9.                 dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z);
    10. }

    Code i added to make the ball feel less floaty:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (rb2d.velocity.y < 0)
    4.         {
    5.             rb2d.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    6.         }
    7.         else if (rb2d.velocity.y > 0)
    8.         {
    9.             rb2d.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
    10.         }
    11.  
    12.     }

     
    Last edited: May 11, 2019