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

Do you remember old "Arcade Volley" videogame? A.I. Logic for opponent?

Discussion in 'General Discussion' started by Feretro, Aug 20, 2021.

  1. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hello!
    I'd like to make a simple "Arcade Volley" Like game. I was wondering what's the "logic" in the opponent script. You know ... following the ball, hitting (forward or backwards?) when to jump ...

    how would you do a script for the enemy? I don't mean the code, (right now) ...

    thanks for your opinions!
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    you can calculate precisely where the ball is going to fall using physics formulas.

    Vertical movement uses formula for accelerated movement, and horizontal movement is linear.
    So just move the opponent to that point and here you go.
     
  3. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Thanks for your reply!

    ... so for the X axis I can "follow" the ball's X axis ... but I am too dumb to use that formula .. I think!

    It would be difficult to know if opponent should hit the ball forward or backward (using a bounce with the wall to send the ball to the other side of the field!)
     
  4. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hello! And Sorry again ... How can I know what point on the X axis the ball is going to fall? It's important to me so I can calculate an "offset" to put the player a little behind the ball ... I am using something like:

    Code (CSharp):
    1.         if (inFront) { offsetPalla = -0.6f; } if (!inFront) { offsetPalla = 0.6f; }
    2.  
    But it's in a fixed update, so my opponent seem to have Parkinson's disease!!
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    The ball has velocity and the scene has gravity.

    Using velocity, initial ball height solve vertical movement for time.

    y1 = startPos.y + startVelocity.y * t - gravity * t * t / 2.

    Solve this for t. Then you'll know how long the ball will stay airborne.
    This is school level formula for accelerated movement.
    https://www.schoolphysics.co.uk/age14-16/Mechanics/Motion/text/Equations_of_motion/index.html

    Now, here's the fun stuff. Gravity does not affect horizontal movement. So you have

    x1 = startPos.x + startVelocity.x * t;

    And that's it. Use t you've just foun and you'll know where it is going to land.
     
    MadeFromPolygons likes this.
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194


    You could just try and match the x position of the ball, with a stay in the middle offset depending on the balls height (to allow for back wall bounce) and if it is over the other side of the net.
     
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,877

    Sounds like you are setting a position of something in fixed update, probably directly via the transform. Dont do that.

    Regarding the rest: Take a look at what @neginfinity wrote as thats everything you need to know
     
    Feretro likes this.