2d Wander

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

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.

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.

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.

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.

Try this example script.

var timer : float = 0;
var tmrSpeed : float = 20;
var movSpeed : float = 20;
var tmrState : int = 0;
var tmrMax : int = 1; 
var movDir : int = 0;

var vTwo : Vector2;

var moveStyle : mvS; enum mvS { eightDir, randDir, sleep }

//=======================================================================// Up D
function Update() {
    //----//
    if(tmrState == 0) {
        //----//
        timer += 0.1 * Time.deltaTime * tmrSpeed;
        if(timer >= tmrMax) {
            timer = 0.0; 
            if(moveStyle == moveStyle.eightDir) { 
                movDir = Random.Range(0, 8); 
                GetPreVector( movDir ); // 8 Dir
            }
            else if(moveStyle == moveStyle.randDir) {
                var ranX = Random.Range(-1.0, 1.0); var ranY = Random.Range(-1.0, 1.0);
                vTwo.x = ranX; vTwo.y = ranY; // ( Vip ) 
            }
            else if(moveStyle == moveStyle.sleep) {
                vTwo.x = 0; vTwo.y = 0; // ( Vip ) 
            }
        }
        //----//
        RandomDirMotor( vTwo );
        //----//
    }
    //----//
}
//=======================================================================// 8 Dir
function GetPreVector( mDir : int ) {
    //----//
    var nV = new Vector2(0,0);
    //----//
    if(mDir == 0) { nV.x = 0; nV.y = 1.0; } // Up
    if(mDir == 1) { nV.x = 0; nV.y = -1.0; } // Down
    if(mDir == 2) { nV.x = -1.0; nV.y = 0; } // Left
    if(mDir == 3) { nV.x = 1.0; nV.y = 0; } // Right
    if(mDir == 4) { nV.x = 1.0; nV.y = 1.0; } // UpRight
    if(mDir == 5) { nV.x = 1.0; nV.y = -1.0; } // DownRight
    if(mDir == 6) { nV.x = -1.0; nV.y = -1.0; } // DownLeft
    if(mDir == 7) { nV.x = -1.0; nV.y = 1.0; } // UpLeft
    //----//
    vTwo.x = nV.x; vTwo.y = nV.y; // ( Vip ) 
    //----//
}
//=======================================================================// Mov
function RandomDirMotor( vTwo : Vector2 ) {
    //----//
    var v2X = vTwo.x * Time.deltaTime * movSpeed;
    var v2Y = vTwo.y * Time.deltaTime * movSpeed;
    transform.Translate( v2X, v2Y, 0 ); // Movement
    //----//
}
//=======================================================================//

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