Search Unity

Vehicle Soccer AI

Discussion in 'Game Design' started by shahzaib, Nov 9, 2016.

  1. shahzaib

    shahzaib

    Joined:
    Dec 14, 2013
    Posts:
    32
    Hi,

    I'm new to Unity but this never seizes my enthusiasm for unity from going to places that I absolutely know nothing about. I've been working on Unity since last 4 (or 5) months, and feel pretty comfortable with Unity, however, I've just started taking vectors more seriously, even though they've always seemed too wild to be tamed.


    Found this gif (above) somewhere on Reddit and I instantaneously fell in love with it. From last few days I'm trying to make similar behavior working but of no use. I'm trying vehicle prefab that comes with unity's standard assets, I'll be more than thankful if somebody could help me out on it.

    Best regards.
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    From looking at the gif there are a two stages for the UI. Looks like there is behaviour for when the car is on the wrong side (Goal, AI, Ball, Home Goal). It looks like its placing a target at a diagonal distance away from the ball on the same side as the car. The distance from the red cube (the target) to the ball is proportional of the distance between the AI car and the ball.

    Once the car is on the correct side of the ball (Goal, Ball, AI, Home Goal), the target switches to a target behind the ball in a line so that hitting the ball in the target line should knock the ball on a goal scoring line. As the ball moves, this target moves too so its always drawing a line toward the Goal. Similar to the first state, the distance between the target and ball seems proportional of the distance between the car and ball.
     
    shahzaib likes this.
  3. shahzaib

    shahzaib

    Joined:
    Dec 14, 2013
    Posts:
    32
    So how can I get the target to always stay behind the ball and face the goal while staying proportional to the vehicle distance at the same time? Where to begin with?

    Thanks elaborating, much appreciated.
     
  4. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    The target will be a position in space that is relative to the ball. Its distance from the ball will be a distance related to how far the AI is from the ball. Its "position" is relative to which team the AI is on. Since there are only 2 teams, you could literally have a "Team1?" style boolean and put it on the side appropriate to the car's assigned team.

    This will simply be drawing a line between the ball and goal, then extending that line out behind the ball to where you want your target vector.

    Get the distance from the Ball object to the AI car object. Then using the line i mentioned in the above point and apply the correct distance there from the ball along that line away from the goal. You might want some constraints on the max distance the target is from the ball as there could be occasions where the car is very far away and the target doesn't need to go too far behind. You might also begin this target distance from the edge of the ball rather than the centre, but that could be as simple as finding the ratio then adding the radius of the ball.

    I don't know the exact ratio however, so you'll need to test this and see. Similar to the gif you could draw the target as a red cube so you can visualise it and then remove that cube when you are happy.
     
    shahzaib likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check out steering behaviours.

    If you really are interested in game AI, check out Mat Buckland's Programming Game AI by Example. It's the definitive primer for game AI techniques.
     
    shahzaib likes this.
  6. shahzaib

    shahzaib

    Joined:
    Dec 14, 2013
    Posts:
    32
    Thanks, BoredMormon, is it for Unity C#?
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No. Its in C++ for console apps. However the principles are all still valid.

    You shouldn't be tackling AI until you have a firm enough grasp of C# that you can write it on your own from C++ pseudo code.