// Update is called once per frame void Update() { pathTime += Time.deltaTime; if (pathTime > 0.5f) { pathTime = 0f; /* * Vector3 targetPos = target.transform.position; // 攻击目标的世界坐标 * Vector3 offset = (this.transform.position - targetPos).normalized * keepGap; * * this.GetComponent<NavMeshAgent>().destination = targetPos + offset; */ SlotManager slotManager = target.GetComponent <SlotManager>(); if (slotManager != null) { if (slot == -1) { slot = slotManager.Reserve(gameObject); } if (slot == -1) { return; } NavMeshAgent agent = this.GetComponent <NavMeshAgent>(); if (agent == null) { return; } agent.destination = slotManager.GetSlotPosition(slot); } } }
// Update is called once per frame void Update() { //每0.5秒刷新进入If分支 pathTime += Time.deltaTime; if (pathTime > 0.5f) { pathTime = 0f; //获得 SlotManager 脚本,为空怎不进入下面If分支 SlotManager slotManager = target.GetComponent <SlotManager>(); if (slotManager != null) { //存储获取槽位数 if (slot == -1) { slot = slotManager.Reserve(gameObject); } //如果没有槽了即返回 if (slot == -1) { return; } //获取 NvaMeshAgent,没有则返回,否则设置目标位置移动 var agent = GetComponent <NavMeshAgent>(); if (agent == null) { return; } agent.destination = slotManager.GetSlotPosition(slot); } } }
//---------------------------------------------------------------------------------------------- // EnterStateInit() is used to initialize the adolescent when they enter the charge state public override void EnterStateInit() { // get a slot position Vector3 closestplayer = npcAdolescentController.GetClosestPlayer(); closestPlayerSlots = npcAdolescentController.GetPlayerSlotManager(npcAdolescentController.PlayerTarget); closestPlayerSlots.ClearSlots(npcAdolescentController.gameObject); availSlotIndex = closestPlayerSlots.ReserveSlotAroundObject(npcAdolescentController.gameObject); if (availSlotIndex != -1) { // if the available slot index isn't a non-existent number, assign it to the destPos destPos = closestPlayerSlots.GetSlotPosition(availSlotIndex); } else { //otherwise, assign the destPos to be the player's position destPos = closestplayer; } elapsedTime = 0.0f; }
//---------------------------------------------------------------------------------------------- // Reason() is used to determine the conditions to transition out of the charge state, // and what state to transition into public override void Reason() { Transform adolescentTransform = npcAdolescentController.transform; Vector3 closestplayer = npcAdolescentController.GetClosestPlayer(); closestPlayerSlots = npcAdolescentController.GetPlayerSlotManager(npcAdolescentController.PlayerTarget); if (health && health.IsDead()) { //if the health script is present on the adolescent and its dead, transition to the dead state npcAdolescentController.PerformTransition(Transition.NoHealth); return; } bool playersDead = true; for (int i = 0; i < 2; i++) { if (playerHealths[i] && !playerHealths[i].IsDead()) { playersDead = false; break; } } if (playersDead) { npcAdolescentController.PerformTransition(Transition.PlayerDead); return; } if (IsInCurrentRange(adolescentTransform, closestplayer, NPCAdolescentController.FLEE_DIST)) { if ((health && health.CurrentHealth <= 10) || GameManager.instance.AdolescentCount == 1) { npcAdolescentController.PerformTransition(Transition.InDanger); return; } } // Keep track of the player slots and release and grab new one every so often... elapsedTime += Time.deltaTime; if (elapsedTime > intervalTime) { elapsedTime = 0.0f; closestPlayerSlots.ReleaseSlot(availSlotIndex); availSlotIndex = closestPlayerSlots.ReserveSlotAroundObject(npcAdolescentController.gameObject); if (availSlotIndex != -1) { destPos = closestPlayerSlots.GetSlotPosition(availSlotIndex); } else { destPos = closestplayer; } } if (IsInCurrentRange(adolescentTransform, closestplayer, NPCAdolescentController.CHASE_DIST)) { // want to check if we are close to our destination position and then transition to attack if (IsInCurrentRange(adolescentTransform, closestplayer, NPCAdolescentController.SLOT_DIST)) { npcAdolescentController.PerformTransition(Transition.ReachPlayer); } } else { //lost player npcAdolescentController.PerformTransition(Transition.LostPlayer); } }