Pong Clone: Any useful Unity functions I can use to predict a ball's position?

I want to implement a hard level AI for my computer paddle in this Pong clone I got here. I do have some initial logic for how this would work, but with what I know of Unity by now, I think it’ll be so hard to program.

What I want the computer paddle to do when it senses a ball at a certain radius, towards it, is to predict exactly where the ball will be when it reaches the paddle’s hit zone, being the region where the paddle can move freely up and down in order to hit the ball back.

50% of the time, the paddle would move to a position where the ball hits one end of it, and the other 50% of the time, the ball hits the paddle’s body while moving. (I added a simple effect where the ball’s movement is affected when the ball is bounced back while the paddle is moving)

There’s one simple case with this, and that is when the ball is moving directly to the opponent’s zone without touching the north or south walls.

For the other cases, though, there are a few steps I need to work through in a function in order to determine a predicted y position for the paddle:

  1. Use raytracing to fetch the collision with the north or south wall, with the ball’s trajectory involving at least one contact with the wall.

  2. Get the normal of the collision point and call Vector3.Reflect to get the next movement vector.

  3. Extend the vector by a certain scalar to see if the vector interacts with either the north or south wall, or the opponent’s dead zone. (the dead zone being the zone that’s between the paddle’s opposing side and the goal zone for which a collision trigger is thrown)

  4. If the extended vector only results in a collision with a wall, then repeat the past two steps until the the resulting vector goes past a certain x value at a certain y range.

  5. Set that target y value somewhere so that the paddle can reference it and move it over there to its direct center.

This seems so complicated to program and probably execute; even though I am only calling this function once the ball hits the detection trigger, there are so many calculations involved that I could get a few frame skips or some lag. I could simply limit this to just a look-ahead where we only look at the reflecting vector from the first collision and move the paddle once the reflecting vector senses the opponent’s zone.

I am wondering if the Unity API has some special functions for this. Otherwise, I am still a newbie with Unity 5 and game AI. The only thing I implemented so far in AI was changing the radius of the ball detector and the paddle’s acceleration. Please let me know if you want to see what is the AI code I got so far.

There is no “specialized” function, that I know of, for these calculations. But the calculus is not that complicated. In order to avoid unnecessary long calculations, you could limit the calculations to one rebound.

So, if you detect the ball is going to hit the wall, you calculate the reflected vector, and repeat the calculations. If you detect the reflected vector will hit the wall again, just skip any more calculations for this frame. That way, the calculation of the position where the ball will hit the goal zone will be deferred to the frame just after the penultimate rebound.

Of course, this can be parameterized to two, or three rebounds, if you like.

Another desirable optimization is to skip calculations when the outcome is already known. So, when you already have the position in the goal zone where the ball is going to hit, you don’t need to calculate anymore.