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

2d Wander

Discussion in 'Scripting' started by ostrich160, May 29, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Alright guys, so Im playing with the (not so now) new 2d feature in unity finally, and making a little game. Now, a crucial feature of this game is wandering AI, and thats about the only thing I havent figured out. Its sidescroller with no jumping, so its just left and right. But I cant think of a way to...

    1). Have AI randomly wander around the small map, changing direction at different times
    2). Avoiding obstacles such as the end of the map
    3). Flipping sprites when it moves in a different direction, so for example when it moves left its facing left
    (I have already done that with my movement script, it uses
    )

    So, it may be a big ask, Im not entirely sure because AI is new to me (used to making multiplayer games). Movement can just be the very simple translations, no need for fancy rigibody physics based movement.

    Thanks guys
     
    Last edited: May 29, 2014
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Well a waypoint system may help then your ai wont be trapped all in one corner. You can always make the path the ai takes on the waypoints random.

    You may run into trouble flipping the EularAngles instead of flipping the animations. The mecanim 2d blend trees help with all that pretty good.
     
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Issue is, it does need to be completely random, there needs to be a way that you cant tell them out very obviously from the actual player.
     
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You could create a timer in update that resets itself in a random time. Each time it resets itself you could fire off a random int that reps ( ie ) eight different directions. As an all to set directions you could set random values in a Vector2.
     
  5. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Im not entirely sure what you mean, or more to the point, how I would go about doing that

    By the way, if I dont respond in a long time its because Im just going to bed now, but if you come up with any ideas that would be fantastic mate.
     
  6. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Try this example script.

    Code (csharp):
    1.  
    2. var timer : float = 0;
    3. var tmrSpeed : float = 20;
    4. var movSpeed : float = 20;
    5. var tmrState : int = 0;
    6. var tmrMax : int = 1;
    7. var movDir : int = 0;
    8.  
    9. var vTwo : Vector2;
    10.  
    11. var moveStyle : mvS; enum mvS { eightDir, randDir, sleep }
    12.  
    13. //=======================================================================// Up D
    14. function Update() {
    15.     //----//
    16.     if(tmrState == 0) {
    17.         //----//
    18.         timer += 0.1 * Time.deltaTime * tmrSpeed;
    19.         if(timer >= tmrMax) {
    20.             timer = 0.0;
    21.             if(moveStyle == moveStyle.eightDir) {
    22.                 movDir = Random.Range(0, 8);
    23.                 GetPreVector( movDir ); // 8 Dir
    24.             }
    25.             else if(moveStyle == moveStyle.randDir) {
    26.                 var ranX = Random.Range(-1.0, 1.0); var ranY = Random.Range(-1.0, 1.0);
    27.                 vTwo.x = ranX; vTwo.y = ranY; // ( Vip )
    28.             }
    29.             else if(moveStyle == moveStyle.sleep) {
    30.                 vTwo.x = 0; vTwo.y = 0; // ( Vip )
    31.             }
    32.         }
    33.         //----//
    34.         RandomDirMotor( vTwo );
    35.         //----//
    36.     }
    37.     //----//
    38. }
    39. //=======================================================================// 8 Dir
    40. function GetPreVector( mDir : int ) {
    41.     //----//
    42.     var nV = new Vector2(0,0);
    43.     //----//
    44.     if(mDir == 0) { nV.x = 0; nV.y = 1.0; } // Up
    45.     if(mDir == 1) { nV.x = 0; nV.y = -1.0; } // Down
    46.     if(mDir == 2) { nV.x = -1.0; nV.y = 0; } // Left
    47.     if(mDir == 3) { nV.x = 1.0; nV.y = 0; } // Right
    48.     if(mDir == 4) { nV.x = 1.0; nV.y = 1.0; } // UpRight
    49.     if(mDir == 5) { nV.x = 1.0; nV.y = -1.0; } // DownRight
    50.     if(mDir == 6) { nV.x = -1.0; nV.y = -1.0; } // DownLeft
    51.     if(mDir == 7) { nV.x = -1.0; nV.y = 1.0; } // UpLeft
    52.     //----//
    53.     vTwo.x = nV.x; vTwo.y = nV.y; // ( Vip )
    54.     //----//
    55. }
    56. //=======================================================================// Mov
    57. function RandomDirMotor( vTwo : Vector2 ) {
    58.     //----//
    59.     var v2X = vTwo.x * Time.deltaTime * movSpeed;
    60.     var v2Y = vTwo.y * Time.deltaTime * movSpeed;
    61.     transform.Translate( v2X, v2Y, 0 ); // Movement
    62.     //----//
    63. }
    64. //=======================================================================//
    65.  
    66.  
     
    Last edited: May 29, 2014
  7. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    This is fantastic! I just had a few issues with it, I guess they'd be an easy fix
    1). Is there anyway to make this just left and right, because at the moment even with random direction, it seems to be quite bumpy, so just a way to make it smoothly go in just the left and right direction
    2). Is there a way to make it avoid the 2d wall colliders, and not just walk into them
    3). Is there a way to make them randomly stop for a few seconds, then go off again

    Ever so sorry if Im asking a lot, because again this script is almost perfect for me, just need to sort out these small issues.

    Thanks mate