public void RestoreState(object state) { SaveableAgent savedState = (SaveableAgent)state; // Reapply saved position GetComponent <NavMeshAgent>().enabled = false; transform.position = savedState.position.ToVector(); GetComponent <NavMeshAgent>().enabled = true; state = savedState.currentState; }
public void OnTargetDetection(GameObject spottedTarget) { target = spottedTarget; // Record last known position of player targetLastKnownPosition = spottedTarget.transform.position; if (state != AGENT_STATE.CHASE) { // Chase Target state = AGENT_STATE.CHASE; OnTargetSpotted.Invoke(); } }
void Update() { if (health.IsDead()) { GetComponent <CapsuleCollider>().enabled = false; GetComponent <Battler>().enabled = false; GetComponent <NavMeshAgent>().enabled = false; // Can cause bugs if enemy dies in the air GetComponent <Rigidbody>().isKinematic = true; state = AGENT_STATE.DEAD; return; } HandleFSM(); }
// CHASE void Chase() { Debug.Log("target:::" + target); if (target == null) { return; } MakeTargetStrafe(); Vector3 destination = targetLastKnownPosition; float currentDistance = Vector3.Distance(target.transform.position, transform.position); // Has reached target if (currentDistance <= stoppingDistanceToPlayer) { state = AGENT_STATE.FIGHTING; return; } // Player escaped if (currentDistance > maxChasingDistance) { state = AGENT_STATE.PATROL; return; } // Has Stamina To Continue chase? if (stamina.HasStaminaAgainstCostAction(staminaChasingCost)) { AI_Helpers.MoveTo(destination, maxSpeed, patrolSpeedFraction, navMeshAgent); stamina.DecreaseStamina(staminaChasingCost); } else { // Stamina depleted return; } }
public SaveableAgent(Vector3 position, AGENT_STATE currentState) { this.position = new SerializableVector3(position); this.currentState = currentState; }
// PUBLIC public void SetState(AGENT_STATE state) { this.state = state; }