void FixedUpdate() { if (droneRacer.Crashed == true) { if (makingCrashSound == false) { makingCrashSound = true; StartCoroutine(InvokeCrashSound()); } return; } if (droneRacerArm.Armed() == false) { motorSound.Stop(); //Debug.Log("motorSound.Stop()"); return; } makingCrashSound = false; float speed = droneRacer.MotorSpeed; if (MathHelper.ValueInDeadZone(speed, 0.1f) == false) { if (motorSound.isPlaying == false) { motorSound.Play(); //Debug.Log("motorSound.Play()"); } motorSound.pitch = speed + minPitch; motorSound.volume = speed * Mathf.Clamp(motorVolume, 0, 1); } else { motorSound.Stop(); } }
void FixedUpdate() { metersPerSecond = droneRigidbody.velocity.magnitude / Constants.Scale; feetPerSecond = metersPerSecond * Constants.Meter2Foot; kilometersPerHour = metersPerSecond * Constants.MetersPerSec2KilometersPerHr; milesPerHour = metersPerSecond * Constants.MetersPerSec2MilesPerHr; if (crashed == false && (droneRacerArm.Armed() == true || joystickInput.Gamepad == true)) { if (thrust > 0 || airMode == true) { if (altitudeHold == true) { Ray ray = new Ray(transform.position, -Vector3.up); RaycastHit hit; if (Physics.Raycast(ray, out hit, thrust)) { float scaledThrust = thrust * altitudeHoldThrustScalar; float proportionalHeight = Mathf.Max(0, (scaledThrust - hit.distance) / scaledThrust); Vector3 appliedAltitudeHoldForce = Vector3.up * proportionalHeight * altitudeHoldStrength; droneRigidbody.AddForce(appliedAltitudeHoldForce, ForceMode.Acceleration); } } droneRigidbody.AddForce(transform.up * thrust); droneRigidbody.AddRelativeTorque(0f, yawForce, 0f); droneRigidbody.AddRelativeTorque(pitchForce, 0f, 0f); droneRigidbody.AddRelativeTorque(0f, 0f, rollForce); if (autoLevel == true) { Vector3 predictedUp = Quaternion.AngleAxis( droneRigidbody.angularVelocity.magnitude * Time.deltaTime, droneRigidbody.angularVelocity ) * transform.up; Vector3 torqueVector = Vector3.Cross(predictedUp, Vector3.up); float strength = autoLevelStrength * MathHelper.ScaleToRange(torqueVector.magnitude, 0, 1, joystickInput.minRotationRate, joystickInput.rotationRate); droneRigidbody.AddTorque(torqueVector * strength); } zeroThrustSpin1 = null; zeroThrustSpin2 = null; } } if (thrust <= 0 || crashed == true) { if (metersPerSecond > crashFinishedSpeed * 2) { if (zeroThrustSpin1 == null) { zeroThrustSpin1 = zeroThrustSpinPoint[Random.Range(0, 4)]; zeroThrustSpin2 = zeroThrustSpinPoint[Random.Range(0, 4)]; } droneRigidbody.AddTorque((zeroThrustSpin1.up + zeroThrustSpin2.up).normalized); if (crashed == true) { //Debug.Log("crashedFakeGravity=" + crashedFakeGravity); droneRigidbody.AddForce(-Vector3.up * crashingFakeGravity, ForceMode.Impulse); } else { //Debug.Log("noThrustFakeGravity=" + noThrustFakeGravity); //Make drone fall faster if there is no thrust being applied droneRigidbody.AddForce(-Vector3.up * noThrustFakeGravity, ForceMode.Impulse); } } } }