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> /// Called every frame to update this behavor when it is running. /// </summary> public override bool BehaviourUpdate() { if (weapons.WeaponsTargetSelector == null || weapons.WeaponsTargetSelector.SelectedTarget == null) { return(false); } UpdateDecisionInfo(); UpdateFiring(); // Attack Vector3 perpendicularTargetDirection = Quaternion.Euler(0f, 90f, 0f) * decisionInfo.toTargetVector; float broadsideAmount = Mathf.Clamp(2 - (decisionInfo.distanceToTarget / broadsideDistanceToTarget), 0, 1); // Update the steering target Vector3 steeringTarget = vehicle.transform.position + (broadsideAmount * perpendicularTargetDirection + (1 - broadsideAmount) * decisionInfo.toTargetVector); // Used to make the ship slow down as it approaches the target float amountOfEngageDistance = (decisionInfo.distanceToTarget - minMaxEngageDistance.x) / (minMaxEngageDistance.y - minMaxEngageDistance.x); // Set the throttle values float forwardThrottleAmount = Mathf.Clamp(amountOfEngageDistance, 0.25f, 1); Maneuvring.TurnToward(vehicle.transform, steeringTarget, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.steeringPIDController.GetControlValues()); engines.SetTranslationThrottleValues(new Vector3(0f, 0f, forwardThrottleAmount)); return(true); }
public override bool BehaviourUpdate() { if (!base.BehaviourUpdate()) { return(false); } if (patrolRoute == null || patrolRoute.PatrolTargets.Count == 0) { return(false); } //Look for patrol route if (Vector3.Distance(vehicle.transform.position, patrolRoute.PatrolTargets[patrolTargetIndex].position) < maxPatrolTargetArrivalDistance) { patrolTargetIndex++; if (patrolTargetIndex >= patrolRoute.PatrolTargets.Count) { patrolTargetIndex = 0; } } Maneuvring.TurnToward(vehicle.transform, patrolRoute.PatrolTargets[patrolTargetIndex].position, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.steeringPIDController.GetControlValues()); engines.SetTranslationThrottleValues(new Vector3(0, 0, patrolThrottle)); return(true); }
/// <summary> /// Stop the input. /// </summary> public override void StopInput() { base.StopInput(); // Reset the space vehicle engines to idle if (spaceVehicleEngines != null) { // Set steering to zero rotationInputs = Vector3.zero; spaceVehicleEngines.SetRotationThrottleValues(rotationInputs); // Set movement to zero translationInputs = Vector3.zero; spaceVehicleEngines.SetTranslationThrottleValues(translationInputs); // Set boost to zero boostInputs = Vector3.zero; spaceVehicleEngines.SetBoostThrottleValues(boostInputs); } }
/// <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); }
// Update is called once per frame void Update() { // Turn toward the lead target position if (targetLeader.Target != null) { // Set the target leader's intercept speed as the current missile speed targetLeader.InterceptSpeed = engines.GetCurrentMaxSpeedByAxis(false).z; Vector3 toTargetVector = targetLeader.Target.transform.position - transform.position; Vector3 adjustedLeadTargetPos = transform.position + (targetLeader.LeadTargetPosition - transform.position).normalized * toTargetVector.magnitude; float leadTargetAngle = Vector3.Angle(toTargetVector, adjustedLeadTargetPos - transform.position); float amount = Mathf.Clamp(maxLeadTargetAngle / leadTargetAngle, 0, 1); Vector3 nextTargetPos = amount * adjustedLeadTargetPos + (1 - amount) * targetLeader.Target.transform.position; Maneuvring.TurnToward(transform, nextTargetPos, maxRotationAngles, steeringPIDController); engines.SetRotationThrottleValues(steeringPIDController.GetControlValues()); } else { engines.SetRotationThrottleValues(Vector3.zero); } }
// Set the control values for the vehicle void SetControlValues() { // Values to be passed to the ship float pitch = 0; float yaw = 0; float roll = 0; Vector3 flattenedForward = new Vector3(engines.transform.forward.x, 0f, engines.transform.forward.z).normalized; Maneuvring.TurnToward(engines.transform, engines.transform.position + flattenedForward, new Vector3(0f, 360f, 0f), shipPIDController.steeringPIDController); pitch = shipPIDController.steeringPIDController.GetControlValue(PIDController3D.Axis.X); roll = shipPIDController.steeringPIDController.GetControlValue(PIDController3D.Axis.Z); yaw = -yawInput.FloatValue(); // ************************** Throttle ****************************** Vector3 nextTranslationInputs = engines.TranslationThrottleValues; if (throttleUpInput.Pressed()) { nextTranslationInputs.z += throttleSensitivity.z * Time.deltaTime; } else if (throttleDownInput.Pressed()) { nextTranslationInputs.z -= throttleSensitivity.z * Time.deltaTime; } // Left / right movement nextTranslationInputs.x = strafeHorizontalInput.FloatValue(); // Up / down movement nextTranslationInputs.y = strafeVerticalInput.FloatValue(); engines.SetTranslationThrottleValues(nextTranslationInputs); if (boostInput.Down()) { engines.SetBoostThrottleValues(new Vector3(0f, 0f, 1f)); } else if (boostInput.Up()) { engines.SetBoostThrottleValues(Vector3.zero); } engines.SetRotationThrottleValues(new Vector3(pitch, yaw, roll)); }
public override bool BehaviourUpdate() { if (!base.BehaviourUpdate()) { return(false); } // Add a weave to the evade path Vector3 targetDirection = Weave(evadeDirection, weaveSpeed, weaveRadius); Maneuvring.TurnToward(vehicle.transform, vehicle.transform.position + targetDirection, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.steeringPIDController.GetControlValues()); engines.SetTranslationThrottleValues(new Vector3(0, 0, 1)); return(true); }
/// <summary> /// Called by the control script every frame when this behaviour is running. /// </summary> public override bool BehaviourUpdate() { // Do collision avoidance float obstacleScanDistance = vehicle.CachedRigidbody.velocity.magnitude * minMaxCollisionReactionTime.y; RaycastHit[] hits = Physics.SphereCastAll(vehicle.transform.position, sphereCastRadius, vehicle.CachedRigidbody.velocity.normalized, obstacleScanDistance); UpdateObstacleData(hits); if (obstacleAvoidanceStrength < 0.01f) { return(false); } else { Maneuvring.TurnToward(vehicle.transform, vehicle.transform.position + obstacleAvoidanceDirection, maxRotationAngles, shipPIDController.steeringPIDController); engines.SetRotationThrottleValues(shipPIDController.steeringPIDController.GetControlValues()); return(true); } }