Basic AI Script Help

Hi I just need some help with AI scripting in Unity 3. My class is creating a game demo and we need to come up with the AI scripts that will make the enemy (a) follow the player and (b) attack the player if in range. Some of the enemies have multiple attacks so I need a script that will allow them to choose between them as well. I've checked out many of the other posts and tutorials but found them to be little help. Thanks!

Making an AI dodge obstacles and/or follow paths is not a simple task; I'd suggest searching the forum for pathfinding and check out an A* (A-star) algorithm or two.

But here's a very very simple implementation. Slap this on an enemy and point its target to the player and it'll chase them in a straight line and play animations named Attack0, 1, 2, etc. based on the numberOfAttacks variable.

var target : GameObject;
var speed : float;
var range : float;
var numberOfAttacks : int;

private var rangeSquared : float;

function Start() {
  rangeSquared = range*range;
}

function Update() {
  if ( target ) {
    var moveDirection : Vector3 = target.position - transform.position;
    moveDirection = moveDirection.normalized * speed;
    transform.position += moveDirection;
    if ( (transform.position - target.position).sqrMagnitude <= rangeSquared ) {
      PerformAttack( Random.value * numberOfAttacks );
    }
  }
}

function DoAttack( attack : int ) {
  animation.Play( "Attack" + attack );
}