/// <summary> /// Called when the enemy updates its Navigation Mesh Path /// </summary> /// <param name="destination">The new target for the enemy</param> private void SetDestination(Vector3 destination) { _pathToNextPoint = new NavMeshPath(); Vector3 height = new Vector3(0, FairyCharacter.transform.position.y - FairyCharacter.GroundCheck.transform.position.y, 0); NavMesh.CalculatePath(FairyCharacter.transform.position - height, destination, NavMesh.AllAreas, _pathToNextPoint); _pathCorners = FairyOperations.CheckArrayForDuplicates(_pathToNextPoint.corners, FairyCharacter.GroundCheck.transform.position).ToArray(); _cornerIndex = 0; }
/// <summary> /// Send the projectile moving towards its target /// </summary> /// <param name="launcher">The object where the projectile is created</param> /// <param name="target">The target for the projectile to hit</param> /// <param name="damage">The amount of damage the projectile will do</param> public void FireProjectile(GameObject launcher, GameObject target, int damage) { if (launcher & target) { _targetDirection = (target.transform.position - launcher.transform.position).normalized; _hasFired = true; this.transform.rotation = FairyOperations.TurnTowardPlayer(this.transform, target.transform); // Destroy the projectile after it has existed for a certain amount of time Destroy(gameObject, 5.0f); } }
public override void UpdateState() { if (EnteredState) { _timeSinceLastUpdate += Time.deltaTime; if (_cornerIndex <= _pathCorners.Length - 1) { // Get the next movement vector, and if true then the corner will be reached if (FairyOperations.GetNextPosition(_pathCorners[_cornerIndex], FairyCharacter.GroundCheck.transform.position, FairyCharacter.MovementSpeed, out Vector3 nextMovement)) { // Prepare to move to next corner on next update if (_cornerIndex <= _pathCorners.Length - 1) { _cornerIndex += 1; } } // Check that the enemy is not moving too close to the player if (FairyOperations.CheckNextPosition(nextMovement, FairyCharacter.transform.position, FairyCharacter.Player.transform.position, 2.0f)) { // Get a new point to move to if (FairyOperations.RandomPoint(FairyCharacter.Player.transform.position, 10.0f, out Vector3 point)) { SetDestination(point); } } // Move towards the new position and look towards it too else { FairyCharacter.transform.position += nextMovement; Vector3 nextLook = new Vector3(nextMovement.x, 0, nextMovement.z); if (!nextLook.Equals(new Vector3(0, 0, 0))) { FairyCharacter.transform.rotation = Quaternion.LookRotation(nextLook); } } } // Check if the enemy should update its path if ((_cornerIndex >= _pathCorners.Length) || (_timeSinceLastUpdate >= _timeBetweenDestinationUpdates)) { SetDestination(FairyCharacter.Player.transform.position); } } }
/// <summary> /// Called when the enemy updates its pathfinding /// Sets a random amount of time before the enemy should update its destination again /// </summary> /// <param name="destination">The new target for the enemy to chase</param> private void SetDestination(Vector3 destination) { if (!FairyCharacter.FiredLastTurn && FairyOperations.chanceToFire(FairyCharacter.transform, FairyCharacter.Player.transform)) { Fsm.EnterState(FsmStateType.Attack); } else { _timeSinceLastUpdate = 0.0f; _timeBetweenDestinationUpdates = Random.Range(0.5f, 2.0f); _pathToTarget = new NavMeshPath(); Vector3 height = new Vector3(0, FairyCharacter.transform.position.y - FairyCharacter.GroundCheck.transform.position.y, 0); NavMesh.CalculatePath(FairyCharacter.transform.position - height, destination, NavMesh.AllAreas, _pathToTarget); _pathCorners = FairyOperations.CheckArrayForDuplicates(_pathToTarget.corners, FairyCharacter.GroundCheck.transform.position).ToArray(); _cornerIndex = 0; FairyCharacter.FiredLastTurn = false; } }
public override void UpdateState() { if (EnteredState) { if (FairyCharacter.PlayerNearby()) { Fsm.EnterState(FsmStateType.Chase); } if (_cornerIndex <= _pathCorners.Length - 1) { // Get the next movement vector, and if true then the corner will be reached if (FairyOperations.GetNextPosition(_pathCorners[_cornerIndex], FairyCharacter.GroundCheck.transform.position, FairyCharacter.MovementSpeed, out Vector3 nextMovement)) { // Prepare to move to next corner on next update if (_cornerIndex <= _pathCorners.Length - 1) { _cornerIndex += 1; } } // Move towards the new position and look towards it too FairyCharacter.transform.position += nextMovement; Vector3 nextLook = new Vector3(nextMovement.x, 0, nextMovement.z); if (!nextLook.Equals(new Vector3(0, 0, 0))) { FairyCharacter.transform.rotation = Quaternion.LookRotation(nextLook); } } // Check if the enemy should stop if (_cornerIndex >= _pathCorners.Length) { Fsm.EnterState(FsmStateType.Idle); } } }
public override void UpdateState() { if (EnteredState) { if (FairyCharacter.PlayerVisible()) { _fireTimer += Time.deltaTime; FairyCharacter.transform.rotation = FairyOperations.TurnTowardPlayer(FairyCharacter.transform, FairyCharacter.Player.transform); if ((_fireTimer >= _firingRate) && (FairyCharacter.Fire())) { FairyCharacter.ChargeAnimationControl.StopChargeAnimation(); Fsm.EnterState(FsmStateType.Chase); } } else { FairyCharacter.ChargeAnimationControl.StopChargeAnimation(); Fsm.EnterState(FsmStateType.Chase); } } }