【Unity 3 D独学記録】ゲーム開発のモンスターAI

2650 ワード

using UnityEngine;
using System.Collections;

public class BoosAI : MonoBehaviour
{

    //    

    //      
    public const int STATE_STAND = 0;
    //    
    public const int STATE_WALK = 1;
    //      
    public const int STATE_RUN = 2;
    //         
    private int enemyState;
    //    
    private GameObject hero;
    //            
    private float backUptime;
    //            
    public const int AI_THINK_TIME = 2;
    //       
    public const int AI_ATTACK_DISTANCE = 10;

    // Use this for initialization
    void Start()
    {
        //      
        hero = GameObject.Find("Cube");
        //           
        enemyState = STATE_STAND;
    }

    // Update is called once per frame
    void Update()
    {
        //          
        if (Vector3.Distance(transform.position, hero.transform.position) <
        (AI_ATTACK_DISTANCE))
        {
            //        
            gameObject.animation.Play("run");
            enemyState = STATE_RUN;
            //          
            transform.LookAt(hero.transform);
        }
        //        
        else
        {
            //         
            if (Time.time - backUptime >= AI_THINK_TIME)
            {
                //      
                backUptime = Time.time;
                
                //  0~2      
                int rand = Random.Range(0,2);

                if (rand == 0)
                {
                    //        
                    gameObject.animation.Play("idle");
                    enemyState = STATE_STAND;
                }
           
            else if (rand == 1)
            {
                //        
                //        
                Quaternion rotate = Quaternion.Euler(0,Random.Range(1,5) * 90,0);
                //1        
                transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * 1000);
                //          
                gameObject.animation.Play("walk");
                enemyState = STATE_WALK;
            }
         }
      }
        switch (enemyState)
        {
            case STATE_STAND:
                break;
            case STATE_WALK:
                //    
                transform.Translate(Vector3.forward * Time.deltaTime);
                break;
            case STATE_RUN:
                //        
                if (Vector3.Distance(transform.position, hero.transform.position) > 3)
                {
                    transform.Translate(Vector3.forward * Time.deltaTime * 3);
                }
                break;

        }
    }
}