Unity ball trajectory 2d for beginners

Hello everyone. This article will be devoted to writing predictions of the ball trajectory in Unity. Unity Version 2019.0.6f

We will use LineRenderer to draw a line.

First, prepare LineRenderer:



What needs to be changed:

  1. Line thickness
  2. Line color
  3. Material

To draw LineRenderer in 2D, you need to create material with the "Sprite / Default" shader:



Create a BallTrajectory script and define the necessary fields.

Field Definition
public class TrajectoryRenderer : MonoBehaviour
{
      [SerializeField] LineRenderer lineRenderer; //  LineRenderer
      [SerializeField] int lineCount; // ,  ,     
      [SerializeField] float lineLenght; // 
      [SerializeField] Ball ball; //   
}


At the start, we set the required number of points for LineRenderer.

private  void  Start ( )
{
      lineRenderer . positionCount  =  lineCount ;
}

In the future, we will write code in the Update function.

First you need to determine the mouse position in the current frame in world coordinates, for this we will use the ScreenToWorldPoint method of the camera class.

Vector2 mousePos  =  Camera . main . ScreenToWorldPoint ( Input . MousePosition ) ;

After that you need to find the normal vector coming from the center of the ball and directed to the mouse position.
Vector2 direction  =  mousePos  -  ( Vector2 ) transform . position ;
direction . Normalize ( ) ;

Next, you need to determine the vertical and horizontal component of the speed of the ball.

Create a ball class. In it, we determine its speed, and by pressing the right mouse button will launch the ball towards the mouse position at a given speed.

Ball script
public class Ball : MonoBehaviour
{
    Rigidbody2D rb;
    [SerializeField] float velocityScaler;
    public float velocity;
 
    Vector2 mousePos;
 
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
 
    private void Update()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        velocity = Vector2.Distance(transform.position, mousePos) * velocityScaler;
 
        if (Input.GetMouseButtonUp(0))
        {
            Vector2 direction = (mousePos - (Vector2)rb.transform.position);
            direction.Normalize();
            rb.velocity = direction * velocity;
        }
    }
}


Ball speed determination
var vx = ball.velocity * direction.x;
var vy = ball.velocity * direction.y;


And finally, we need to go through all the points of our LineRenderer and determine their position in accordance with the received data.

Point positioning
for (int i = 0; i < lineCount; i++)
{
      float t = i * lineLenght;
      var nextPos = new Vector2(vx * t, vy * t - (-Physics2D.gravity.y * t*t) / 2);
      lineRenderer.SetPosition(i, (Vector2)transform.position + nextPos);
}


Full code can be viewed

here
public class BallTrajectory : MonoBehaviour
{
      [SerializeField] LineRenderer lineRenderer;
      [SerializeField] int lineCount;
      [SerializeField] float lineLenght;
      Ball ball;
 
      private void Start()
      {
            ball = GetComponent<BallController>();
            lineRenderer.positionCount = lineCount;
      }
 
      private void Update()
      {
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 direction = mousePos - (Vector2)transform.position;
            direction.Normalize();
 
            var vx = ball.velocity * direction.x;
            var vy = ball.velocity * direction.y;
 
            for (int i = 0; i < lineCount; i++)
            {
                  float t = i * 0.1f;
                  var nextPos = new Vector2(vx * t, vy * t - (-Physics2D.gravity.y * t*t) / 2);
                  lineRenderer.SetPosition(i, (Vector2)transform.position + nextPos);
            }
      }
 
}


Screenshots



Thank you for the sprites looneybits .

All Articles