# How to make basic AI in a 2d game?

Hello:

How can i make a basic AI such as in games like Super Mario Bros >> what i mean making the enemy moving from left to right and Right to left.

if my English was bad ((forgive me)).

You can attach this to a game object in the scene and it will walk along the x axis between 0 and 5. If it is not between 0 and 5 to start with it will walk towards (0,5) and stay there. The reason that Time.deltaTime is used is because each time slice within Update() will be different and you only want to move the object as much time that is passing for that frame (actually the game engine typically uses the time for the previous frame or some other method such as an average, but the details for this aren't important). Also, hitting the walls isn't exact since there will be some overshoot, you can fix that by chopping off the amount that you would go past the wall. I didn't want to complicate the code.

An alternate way is to use Coroutines (rather than Update), but that can be answered elsewhere.

``````using UnityEngine;
using System.Collections;

public class Walker : MonoBehaviour {

public float walkSpeed = 2.0f;
public float wallLeft = 0.0f;
public float wallRight = 5.0f;

float walkingDirection = 1.0f;
Vector3 walkAmount;

// Update is called once per frame
void Update () {

walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;

if (walkingDirection > 0.0f && transform.position.x >= wallRight)
walkingDirection = -1.0f;
else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
walkingDirection = 1.0f;

transform.Translate(walkAmount);
}
}

``````

On Unity3d.com/support/resources/tutorials there is a brilliant tutorial entitled 3rd person platformer game. There are some AI examples and script in that tutorial. Hope this helps!!!

Here is a waypoint based javascript that i learned from steamisM50's youtube channel

(worth checking out for tutorials youtube/streamis50)

## now the actual code:

var waypoint : Transform[];

var speed : float = 20;

private var currentWaypoint : int;

var loop : boolean = false;

var Distance : float = 1;

function Awake(){

``````waypoint[0] = transform; //first waypoint is his starting position

``````

} function Update () {

``````if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;

var velocity = rigidbody.velocity;
if(moveDirection.magnitude < Distance)
{
currentWaypoint++;
}

else{
velocity = moveDirection.normalized * speed;
}
}

else{
if(loop)
{
currentWaypoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;

``````

}

You can apply the following script to the object you’d like moving side to side. Make sure you leave “Vert” unchecked. If you check it, then the object will move up and down instead of side to side. (Useful for other purposes.) You can change the “Max step” var from the inspector to change how far the object moves. Changing the “Step” variable will change how fast the object moves. All you’d need to do is attack an attack function and you’re good to go!

#pragma strict

private var Xpos : float;
private var Ypos : float;
private var max : boolean;

var Vert : boolean;
var maxAmount : int;
var step : float;

function Start ()
{
Xpos = transform.position.x;
Ypos = transform.position.y;
}

function FixedUpdate ()
{

if (Vert)
{
if (transform.position.y >= Ypos + maxAmount)
{
max = true;
}

``````	else if (transform.position.y <= Ypos)
{
max = false;
}
}

else
{
if (transform.position.x >= Xpos + maxAmount)
{
max = true;
}

else if (transform.position.x <= Xpos)
{
max = false;
}
}

if (Vert)
{
if (!max)
{
transform.position.y += step;
}

else
{
transform.position.y -= step;
}
}

else
{
if (!max)
{
transform.position.x += step;
}

else
{
transform.position.x -= step;
}
}
``````

}