Help with AI

Hello everyone, since I started back at college I have been working on my FMP.
Up to now its going well I have setup quite a lot. You can see what I have done already here: MJ - YouTube

I’ve gotten to the point where I need to have my AI avoid buildings, walls, high terrain.

I’ve been looking at using raycasts to do this but I have no idea where to start.

My current AI

//var for health and setting the amount
//static var health : float = 100.0;
//players transform
var player : Transform;
var playerGO : GameObject;
//default speed of the enemy
var speed = 5.0;
//how far the enemy will attack from
var chaseRange = 50.0;
//attacking range
var dieRange = 5.0;
//last time enemy attacked
var lastShotFired = 0;
//how often the enemy can attack
var attackTime = 2;
//setting up var for the enemies controller(Makes it walk)
private var controller : CharacterController;
var bulletTraceGeneratorScript : GameObject;

function Start()
playerGO = GameObject.FindWithTag("Player");
player = playerGO.transform;

bulletTraceGeneratorScript = GameObject.FindWithTag("BulletTrace");
//gets the controller
    controller = GetComponent(CharacterController);
//    yield MyWaitFunction (2.0); (OLD CODE)

function Update()
//if the players var is empty put nothing in there
    if (player == null)
//How far away from the player the enemy is
    var range = Vector3.Distance(player.position, transform.position);
//If the enemy is withtin attacking range(attack)
    if (range <= dieRange)
//if the time is 2(attacktime) past the lastshotfired then attack
   if (Time.time > lastShotFired + attackTime) {
	animation["attack"].speed = 2;
	animation["attack"].wrapMode = WrapMode.Loop;
	lastShotFired = Time.time;
	player.GetComponent("health").health -= 10;


//	yield return new WaitForSeconds(attackTime);(OLDCODE)

//if it isnt in the attacking range
    else if (range >= chaseRange)
    //enenmy looks at the player
    //setting up the var so the enemy knows which way to go
        var moveDirection : Vector3 = transform.TransformDirection(Vector3.forward);
    //moving the enemy toward the player (time.deltaTime*speed - making sure it is moving correctly)

       controller.Move(moveDirection * Time.deltaTime * speed);
     //this checks to see if the enemy is hovering above the ground
     //this is common in most AI as it only moves the enemy straight towards the player
     //this however fires a raycast at the floor which then adjusts the enemies position to
     //the hit point of the raycast
   	//setting up the raycast var   
	var hit : RaycastHit;
    //if the raycast (from the enemy (-up) downwards) hits something below then it will
    //put the value in the hit var
    if (Physics.Raycast (this.transform.position, -Vector3.up, hit)) {
       //this is the distance from the hit point and the enemy
       distanceToGround = hit.transform.position;
       //DEBUG STUFF
       //print("Distance: "+distanceToGround);
       //print("Hitting "+hit.transform);
       //the commented line below draws a line in the editor so that I can check it is working
       //Debug.DrawLine (this.transform.position, hit.point,, 1);
       //this moved the enemies position from where it was(floating) to the floor
       this.transform.position = hit.point;

	animation["RunninInPlace"].speed = 1;
		animation["RunninInPlace"].wrapMode = WrapMode.Loop;
	//if it isnt doing any of the above stand still and play the idle animation
	//plays the idle animation
	//loops the animation
	animation["idle"].wrapMode = WrapMode.Loop;

If anyone has any idea how I could implement raycasts to make my AI avoid stuff please help. I’m really stuck with this.

I don’t want anyone to do this for me. Just give me a way of doing it.

Thanks for any help you can give :slight_smile:


Hi Mike,

“Ray - Casting” is what you would want. Please have a look at these.

And that should help with adjacent objects. stopping at terrain is easy too. Stick an invisible boundary at the terrain with a box collider.