Search Unity

Question Predict ball hit place in Pong game?

Discussion in 'Scripting' started by Quast, Mar 23, 2021.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    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.
    Code (CSharp):
    1.  
    2.     public GameManager _gm;
    3.     public GameObject Ball, middleObject, PredictLineObject;
    4.     public float speed;
    5.     public float BallDistance, AiX, AiZ, BallNum;
    6.     public Vector3 ballPlace, PredictHit;
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.  
    12.         BallNum = _gm.GetComponent<GameManager>().BallinMatch;
    13.  
    14.         if (BallNum == 0)
    15.         {
    16.             Ball = null;
    17.         }
    18.         else
    19.         {
    20.             Ball = GameObject.FindGameObjectWithTag("GameBall");
    21.             BallDistance = Vector3.Distance(gameObject.transform.position, Ball.transform.position);
    22.             ballPlace = new Vector3(transform.position.x, transform.position.y, Ball.transform.position.z);
    23.             transform.position = Vector3.Lerp(transform.position, ballPlace, Time.deltaTime * speed);
    24.         }
    25.  
    26.         AiX = gameObject.transform.position.x - middleObject.transform.position.x;
    27.         AiZ = gameObject.transform.position.z - middleObject.transform.position.z;
    28.  
    29.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    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.