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: http://www.youtube.com/channel/UCrOw2DA0dlAy4oMwIEzFsUA/

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)
            return;
    //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) {
    	
    	Debug.Log("Attacking");
    	animation.Play("attack");
    	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
            transform.LookAt(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, Color.red, 1);
           
           //this moved the enemies position from where it was(floating) to the floor
           this.transform.position = hit.point;
           
        }
    
    	animation["RunninInPlace"].speed = 1;
    	animation.Play("RunninInPlace");
    		animation["RunninInPlace"].wrapMode = WrapMode.Loop;
    		
        }
    	//if it isnt doing any of the above stand still and play the idle animation
    	else
    	{
    	//plays the idle animation
    	animation.Play("idle");
    	//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:

Mike

You can just add this easy step to your raycatsing or tag.

private var LeftorRight : boolean = true;
var speeded : float = 150.0;

if (collision.collider.gameObject.tag == “Wall”)
{
if(LeftorRight)
{
yield WaitForSeconds(1);
transform.Rotate(0, speeded * Time.smoothDeltaTime, 0);
}