您的位置:首页 > 移动开发 > Unity3D

Unity 代码碎片敌人的AI

2013-08-01 15:51 344 查看
using UnityEngine;
using System.Collections;

public class Enemy_AI : MonoBehaviour {
	
	public const int STATE_STAND = 0;
	public const int STATE_WALK  = 1;
	public const int STATE_RUN   = 2;
	
	private int enemyState = 0;
	
	private float backUpTime;
	
	public const int AI_THINK_TIME = 2;
	
	public const int AI_ATTACK_DISTANCE = 20;
	
	private GameObject hero;
	private GameObject bullet;
	
	private float backUPAttackTime ;
	
	
	private TrailRenderer trialRenderer;
	
	
	
	
	// Use this for initialization
	void Start () {
		bullet = GameObject.Find("Bullet");
		

		
		hero = GameObject.Find("Hero");
		enemyState = STATE_STAND;
	}
	
	// Update is called once per frame
	void Update () {
		
		float Length = Vector3.Distance(this.gameObject.transform.position,hero.transform.position);
		
		
		if( Length< AI_ATTACK_DISTANCE )
		{
			this.gameObject.transform.LookAt(hero.transform);
			this.gameObject.animation.Play("run");
			enemyState = STATE_RUN;
			if(Length < 15)
			{
				
				if(Time.time - backUPAttackTime >= AI_THINK_TIME)
				{
					backUPAttackTime = Time.time;
					Attack_AI();
				}
				
				
				
			}
			
		}
		else
		{
			if(Time.time - backUpTime >= AI_THINK_TIME)
			{
				backUpTime = Time.time;
				int rand = Random.Range(0,2);
				if(rand == 0)
				{
					enemyState = STATE_STAND;
					this.gameObject.animation.Play("idle");
				}
				else if(rand == 1)
				{
					Quaternion rotate = Quaternion.Euler(0,Random.Range(1,60) * 6,0);
					
					this.gameObject.transform.rotation = Quaternion.Slerp(this.gameObject.transform.rotation,rotate,Time.deltaTime * 1000);
					
					this.gameObject.animation.Play("walk");
					enemyState = STATE_WALK;
				}
			}
		}
		
		switch(enemyState)
		{
		case STATE_STAND:
			break;
		case STATE_WALK:
			this.gameObject.transform.Translate(Vector3.forward * Time.deltaTime);
			break;
		case STATE_RUN:
			if(Vector3.Distance(this.gameObject.transform.position,hero.transform.position) >= 3)
			{
				this.gameObject.transform.Translate(Vector3.forward * Time.deltaTime * 5);
			}
			break;
			
		}
	}
	
	void Attack_AI()
	{
		GameObject bullet1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
		bullet1.AddComponent("Rigidbody");
		bullet1.name = "Bullet";
	
		bullet1.AddComponent("TrailRenderer");
		trialRenderer = bullet1.GetComponent<TrailRenderer>();
		trialRenderer.startWidth = 0.1f;
		trialRenderer.endWidth = 0.1f;
		trialRenderer.time = 0.5f;
		
		
		
		bullet1.transform.localScale= new Vector3(0.2f,0.2f,0.2f);
		
		bullet1.rigidbody.useGravity = false;
		//find Enemy forward point
		float x = this.gameObject.transform.position.x + this.gameObject.transform.forward.x;
		float z = this.gameObject.transform.position.z + this.gameObject.transform.forward.z;
		
		
		bullet1.transform.position = new Vector3(x ,1,z);
		
		
		bullet1.rigidbody.AddRelativeForce(this.transform.forward * 400);
	}
	
	
	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: