public override bool BehaviourUpdate() { if (!base.BehaviourUpdate()) { return(false); } if (formationTarget == null) { return(false); } // Get the target position Vector3 targetPos = formationTarget.TransformPoint(formationOffset); // Calculate the target to orient the ship toward. As the ship approaches the formation target // position, it must orient itself toward a point far ahead in space to prevent oscillation. float turnTowardAmount = Mathf.Clamp(Vector3.Distance(rBody.position, targetPos) / turnTowardDistance, 0, 1); Vector3 steeringTargetPos = turnTowardAmount * targetPos + (1 - turnTowardAmount) * (targetPos + formationTarget.forward * 1000); // Steer Maneuvring.TurnToward(rBody.transform, steeringTargetPos, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.GetSteeringControlValues()); // Move Maneuvring.TranslateToward(rBody, targetPos, shipPIDController.movementPIDController); Debug.DrawLine(rBody.transform.position, targetPos, Color.red); engines.SetTranslationThrottleValues(shipPIDController.GetMovementControlValues()); return(true); }
/// <summary> /// Update the behaviour. /// </summary> public override bool BehaviourUpdate() { if (!base.BehaviourUpdate()) { return(false); } if (weapons.WeaponsTargetSelector == null || weapons.WeaponsTargetSelector.SelectedTarget == null) { return(false); } Vector3 targetPos = weapons.WeaponsTargetSelector.SelectedTarget.transform.position; Vector3 toTargetVector = targetPos - vehicle.transform.position; // Do primary weapons bool canFirePrimary = Vector3.Angle(vehicle.transform.forward, toTargetVector) < maxFiringAngle && toTargetVector.magnitude < maxFiringDistance; if (canFirePrimary) { // Fire in bursts if (Time.time - primaryWeaponActionStartTime > primaryWeaponActionPeriod) { SetPrimaryWeaponAction(!primaryWeaponFiring); } } else { SetPrimaryWeaponAction(false); } // Do the secondary weapons if (weapons.TargetLockers.Count > 0 && weapons.TargetLockers[0].LockState == LockState.Locked) { if (Time.time - secondaryWeaponActionStartTime > secondaryWeaponActionPeriod) { triggerablesManager.TriggerOnce(1); secondaryWeaponActionPeriod = Random.Range(minMaxSecondaryFiringInterval.x, minMaxSecondaryFiringInterval.y); secondaryWeaponActionStartTime = Time.time; } } // Turn toward target Maneuvring.TurnToward(vehicle.transform, targetPos, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.steeringPIDController.GetControlValues()); Maneuvring.TranslateToward(rBody, targetPos, shipPIDController.movementPIDController); engines.SetTranslationThrottleValues(shipPIDController.movementPIDController.GetControlValues()); return(true); }