Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pong AI NEED HELP!

Discussion in 'Scripting' started by Nick0, Jan 15, 2015.

  1. Nick0

    Nick0

    Joined:
    Jan 15, 2015
    Posts:
    1
    Hey guys I'm a bit new to scripting but anyway I'm almost finished a pong game but I'm not doing very well with this AI. It's a very simple AI I just want the paddle to follow the ball on the y axis but I'm lost on what to script, here's what I've done so far:
    #pragma strict

    var OpponentPaddle : Transform;
    var theBall : Transform;
    var ballSpeed : float;

    function Update () {
    if (theBall.position.y){
    OpponentPaddle.position.y;
    }
    }
    Any help appreciated! Thanks in advance
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    First of all, use code tags please. Secondly, you'd definitely benefit from some tutorials! You'd get your answers much more quickly, and it's a lot more fun to learn.

    This is as simple as assigning a position to your opponent paddel transformation, but let's make it a bit more fun by using Lerp:
    Code (JavaScript):
    1. var opponentPaddle : Transform;
    2. var opponentSpeed : float;
    3.  
    4. var ball : Transform;
    5. var ballSpeed : float;
    6.  
    7. function Update () {
    8.     var targetPosition : Vector3 = new Vector3(opponentPaddle.x, ball.y, 0f);
    9.  
    10.     opponentPaddle.position = Vector3.Lerp(opponentPaddle.position, targetPosition, Time.deltaTime * opponentSpeed);
    11. }
     
    Polymorphik likes this.
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Officer Doofy is on the case! :D
     
    BenZed likes this.