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); } }
相关文章推荐
- Unity3D&AI:寻路(1):A星寻路,在Unity上用C#代码实现。
- Unity5.x制作合金弹头(三)-敌人AI逻辑
- Unity敌人的AI
- Unity 基于状态的简单敌人AI
- [Unity插件]Behavior Designer:制作敌人AI
- unity官方demo学习之Stealth(二十四)敌人AI
- Unity敌人生成点的代码
- Unity 项目中绘制血条的代码碎片
- Unity 内的敌人AI
- [Unity插件][AI]敌人自动攻击AI插件
- unity 武器拖尾效果实现的代码
- [置顶] Unity-AI(Sprite随机运动)
- unity屏幕分辨率设置注意及代码
- 新 Unity 最新粒子系统如何用代码改变参数
- 5.使用 unity GUI代码生成按钮
- unity实现窗口提示代码
- 初级UnityDemo全纪录(二):代码篇③
- 《好书推荐》提高Unity中C#代码质量的xxx条准则
- deelplearning.ai 第二课第二周作业代码
- unity开发android插件(unity调用android代码) – unity3d游戏开发