void FixedUpdate() { if (_thrustDirectionsByPower.Count == 0 || _preferredCannonDirections.Count == 0) { print($"ERROR: Pirate {name} is missing thrust or cannon data"); return; } /* * Each frame, we are going to need to determine: * * - If we are not within our preferred firing distance: * - Find the direction we can rotate that will give us more preferable thrust toward the floatilla until we reach the direction with maximum thrust toward the floatilla. * - Rotate in that direction * - Apply thrust in the direction of the floatilla if we can. If we cannot, apply lateral thrust if we can. * - If cannons are facing the floatilla, fire. * - If we are within our preferred firing distance: * - If we are not facing the floatilla with our preferred cannon direction, find which direction we can rotate * that will give us a more preferrable cannon direction on the way to that preferred direction. * - If cannons are facing the floatilla, fire. * - Find the direction with the most engines toward/lateral to the floatilla, and ignite in that direction. */ Vector3 floatillaMidpoint = _floatilla.GetWorldMidpoint(); Vector3 relativePosition = this.transform.position - floatillaMidpoint; Vector3 relativePositionLocalSpace = this.transform.worldToLocalMatrix.MultiplyPoint(floatillaMidpoint); if (relativePosition.magnitude > this.PreferredDistance) { Vector3 destinationLocalHeading = _thrustDirectionsByPower[0]; AngleTowardOpponentInDirection(relativePositionLocalSpace, destinationLocalHeading); ThrustInDirection(_thrustDirectionsByPower[0]); } else { // Once in range, make the turning preference toward the side with the most cannons Vector3 destinationLocalHeading = _preferredCannonDirections[0]; AngleTowardOpponentInDirection(relativePositionLocalSpace, destinationLocalHeading); // Once in range, thrust in the first engine direction you find that is toward or perpendicular // to the player. foreach (var engineDirection in _thrustDirectionsByPower) { if (Vector3.Angle(engineDirection, relativePositionLocalSpace) < 90.0f) { ThrustInDirection(engineDirection); break; } } } // Fire cannons if facing the right direction foreach (var cannonDirection in _preferredCannonDirections) { if (Vector3.Angle(cannonDirection, relativePositionLocalSpace) < 15.0f) { this.Ship.FireActiveCannons(cannonDirection); } else { this.Ship.StopActiveCannons(cannonDirection); } } }