Predict ball hit place in Pong game?

Hi,
I’m working on PingPong game style. My script for the AI player is working fine. But I want to add feature let him know the place will ball hit it. I add invisible object “PredictLineObject” near AiPlayer so I can use it to give me “PredictHit” values to use it. This is my method I don’t know it will work or not. If you have a better one please write it.

    public GameManager _gm;
    public GameObject Ball, middleObject, PredictLineObject;
    public float speed;
    public float BallDistance, AiX, AiZ, BallNum;
    public Vector3 ballPlace, PredictHit;

    // Update is called once per frame
    void Update()
    {

        BallNum = _gm.GetComponent<GameManager>().BallinMatch;

        if (BallNum == 0)
        {
            Ball = null;
        }
        else
        {
            Ball = GameObject.FindGameObjectWithTag("GameBall");
            BallDistance = Vector3.Distance(gameObject.transform.position, Ball.transform.position);
            ballPlace = new Vector3(transform.position.x, transform.position.y, Ball.transform.position.z);
            transform.position = Vector3.Lerp(transform.position, ballPlace, Time.deltaTime * speed);
        }

        AiX = gameObject.transform.position.x - middleObject.transform.position.x;
        AiZ = gameObject.transform.position.z - middleObject.transform.position.z;

    }

I have stared at your code and I too don’t know if it will work or not.

Please do keep us all informed, I’m surely not the only curious one.

That’s not really how this works.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

Beyond that, if your code above does not quite work, then in order to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.