using UnityEngine;using System.Collections;public class EnemyController : MonoBehaviour { /// <summary> /// 血条类 /// </summary> [HideInInspector] public PlayerHealth health; /// <summary> /// 移动的方向 /// </summary> [HideInInspector] public Vector3 MoveDrection; /// <summary> /// 敌人的状态 /// </summary> // [HideInInspector] public EnemyState enemyState; /// <summary> /// 攻击的目标 /// </summary> [HideInInspector] public Transform attackTarget; /// <summary> /// 动画组件 /// </summary> private Animator anim; /// <summary> /// 角色控制组件 /// </summary> private CharacterController controller; /// <summary> /// 当前动画的状态 /// </summary> private AnimatorStateInfo stateInfo;void Start () { InitFramePanel(); enemyState = EnemyState.Idle; anim = GetComponentInChildren<Animator>(); controller = GetComponent<CharacterController>(); MoveDrection = Vector3.zero;} void FixedUpdate() { //获取当前动画的状态 stateInfo = anim.GetCurrentAnimatorStateInfo(0); switch (enemyState) { case EnemyState.Idle: IdleController(); break; case EnemyState.Move: MoveController(); break; case EnemyState.Attack: AttackController(); break; case EnemyState.Death: DeathController(); break; }} #region 空闲动画脚本IdleController /// <summary> /// 空闲动画脚本IdleController /// </summary> private void IdleController() { if (controller != null && anim != null && enemyState==EnemyState.Idle) { //获取当前动画状态 if (stateInfo.nameHash != Animator.StringToHash("Base Layer.Idle")) anim.SetFloat("Speed", 0); } else Debug.LogWarning("EnemyController没有找到标签为CharacterController的组件或者Animator组件"); } #endregion #region 移动动画脚本MoveController /// <summary> /// 移动动画脚本MoveController /// </summary> private void MoveController() { if (controller != null && anim != null && enemyState==EnemyState.Move) { if (stateInfo.nameHash != Animator.StringToHash("Base Layer.Walk")) anim.SetFloat("Speed", 1); if (MoveDrection != Vector3.zero) controller.Move(MoveDrection); } else Debug.LogWarning("EnemyController没有找到标签为CharacterController的组件或者Animator组件"); } #endregion #region 攻击动画脚本AttackController /// <summary> /// 攻击动画脚本AttackController /// </summary> private void AttackController() { MoveDrection = Vector3.zero; if (controller != null && anim != null && enemyState == EnemyState.Attack) { if (attackTarget != null) { if (stateInfo.nameHash != Animator.StringToHash("Base Layer.Attack1")) anim.SetTrigger("Attack1"); HeroController hero = attackTarget.GetComponent<HeroController>(); hero.health.UpdateCurrentHP(-5); enemyState = EnemyState.Idle; } else Debug.Log("没有攻击目标哎"); } else Debug.LogWarning("EnemyAI没有找到标签为CharacterController的组件或者Animator组件"); } #endregion #region 死亡动画脚本DeathController /// <summary> /// 死亡动画脚本DeathController /// </summary> private void DeathController() { if (controller != null && anim != null && enemyState == EnemyState.Death) { if (stateInfo.nameHash != Animator.StringToHash("Base Layer.Death")) { anim.SetTrigger("Death"); } Debug.LogWarning("呜呜呜呜,我这人见人爱,花见花开的狼宝宝怎么死了呢"); } } #endregion #region 初始化FramePanel /// <summary> /// 初始化FramePanel /// </summary> private void InitFramePanel() { GameObject root = GameObject.Find("UI Root/Anchor_TopRight"); GameObject obj = Resources.LoadAssetAtPath("Assets\Prefabs\Health\PlayerHealth.prefab", typeof(GameObject)) as GameObject; if (root != null && obj != null) { obj = Instantiate(obj) as GameObject; obj.transform.parent = root.transform; obj.transform.localScale = Vector3.one; obj.transform.localPosition = new Vector3(-, -, 0); health = obj.GetComponent<PlayerHealth>(); health.MaxHP = 8; health.deathDelegate = new CallBack(Death); } } #endregion /// <summary> /// 死亡 /// </summary> public void Death() { enemyState = EnemyState.Death; }}public enum EnemyState{ Idle, Move, Attack, Death}
推荐整理分享AI控制器(ai控制器的简称是),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:air控制器,ai控制器是什么,ai控制器检查不到玩家,ai控制器是什么,ai控制器是什么,ai控制器是什么,AI控制器龙头,ai控制器是什么,内容如对您有帮助,希望把文章链接给更多的朋友!
人物移动 voidUpdate(){if(Input.GetMouseButtonDown(0)){RayControl();}if(flagMove){if(Vector3.Distance(transform.position,mousePos)1){transform.Translate(transform.worldToLocalMatrix*transform.forward*Time.delta
围绕摄像机旋转 vartarget:Transform;vardistance=.0;varxSpeed=.0;varySpeed=.0;varyMinLimit=-;varyMaxLimit=;privatevarx=0.0;privatevary=0.0;@scriptAddComponentMenu(Camera-Control/MouseOrbit)functionStart(){
Unity 初级AI躲避障碍物 1.引言阅读《UnityGameAIprogramming》第6章后,感觉躲避障碍物算法不是很给力。为了研究和学习Unity,自己改良躲避障碍物的算法。当然,代码没有优化,