// Initialize FreePlay Target Mode public void InitializeFreePlayTargetMode() { Vector3 SVector = CalculateSVector(); springArrow.SetArrowTransform(cannonBall.SpringPoint_Centered); springArrow.transform.up = SVector.normalized; SpringK = SVector.magnitude; gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint); springArrow.Show(); gravArrow.Show(); CalculateForces(); }
public void ShowArrowIndicator(float effectRange, float angle) { if (this.arrowIndicatorGo == null) { this.arrowIndicatorGo = Instantiate <GameObject>(this.arrowIndicatorPf); this.arrowIndicator = arrowIndicatorGo.GetComponent <ArrowIndicator>(); } arrowIndicator.Show(effectRange, angle); }
public IEnumerator DoProcessPreLaunch_Forces() { Debug.Log("Observe Forces of Gravity, Tension and Spring before launch"); tensionPoint = cannonBall.TensionPoint; weightPoint = cannonBall.LeftWeightPoint; springPoint = cannonBall.SpringPoint_LeftOffset; // Set all arrow starting positions Vector3 SVector = CalculateSVector(); springArrow.SetArrowTransform(springPoint); springArrow.transform.up = SVector.normalized; tensionArrow.SetArrowTransform(tensionPoint); gravArrow.SetArrowTransform(weightPoint); SpringK = SVector.magnitude; // Set default spring force springArrow.Show(); // Show Tensions force tensionArrow.Show(); CalculateForces(); // To ensure latest forces are calculated // Set Gravity Force and show gravArrow.ChangeArrowWeight(cannonBall.WeightForce); gravArrow.Show(); CanProcessNextStep = false; yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step1CamTransform))); CanProcessNextStep = true; // Position the Distance Gizmo and Rotate it around the Catapault arm to match the time at launch position of the cannonball distanceGizmo.transform.position = cannonBall.transform.position; distanceGizmo.transform.RotateAround(catapult.catapultArm.transform.position, -catapult.catapultArm.transform.up, catapult.DEFAULT_LAUNCH_ANGLE); distanceGizmo.transform.rotation = Quaternion.identity; DistanceFromGround_At_TimeOFLaunch = distanceGizmo.transform.position.y - terrain.transform.position.y; // update the distance of the cannonball from the ground at time of launch distanceGizmo.Show(); }
public IEnumerator DoProcessMidAirState_Energy() { Debug.Log("Show transfer of Elastic Potential Energy to Kinetic Energy that Launches the Cannon Ball and Observe Forces while in air"); if (failLaunch) { popup.Show(); yield break; } CanProcessNextStep = false; sliderLockOut = true; // Lockout physics config sliders midAirStep = MidAirStep.Launching; energyWidget.Show(); // Show Kinetic Energy Info float currentKineticEnergy = 0; energyWidget.UpdateKineticEnergy(currentKineticEnergy); // Show Draining of Elastic Potential Energy To Kinetic Energy (Kinetic Energy will equal the Elastic Potential Energy) float kineticEnergy = Kinetic_Energy_At_Launch(); float elasticEnergy = Kinetic_Energy_At_Launch(); const float drainSpeed = 0.05f; // Animate the depletion of Elastic Potential Energy and Gain of Kinetic Energy prior to Launch while (!EEMath.Approximately(elasticEnergy, 0, 0.001f) || !EEMath.Approximately(currentKineticEnergy, kineticEnergy, 0.001f)) { yield return(null); elasticEnergy = Mathf.Lerp(elasticEnergy, 0, drainSpeed); energyWidget.UpdateElasticEnergy(elasticEnergy); currentKineticEnergy = Mathf.Lerp(currentKineticEnergy, kineticEnergy, drainSpeed); energyWidget.UpdateKineticEnergy(currentKineticEnergy); } yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step3CamTransform))); float velocity = Velocity_At_Time_Of_Launch(); float ratio = ratio_Of_DEPE_Over_DGPE(); catapult.ThrowBall(catapult.launchVector.up, velocity); while (!cannonBall.paused) { yield return(null); Transform riseMarker = Utils.CreateMarker(null, 0.1f, Color.red); riseMarker.position = cannonBall.transform.position; markers.Add(riseMarker.gameObject); // Catch when the ball has left max arc height if (cannonBall.rigidBody.velocity.y < 0) { if (!cannonBall.maxHeightReached) { cannonBall.maxHeightReached = true; Transform peakMarker = Utils.CreateMarker(null, 0.2f, Color.blue); peakMarker.position = cannonBall.prevPosition; markers.Add(peakMarker.gameObject); cannonBall.PauseInAir(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform))); } } } midAirStep = MidAirStep.Maxheight; weightPoint = cannonBall.LeftWeightPoint; resultPoint = catapult.springVector; ArrowIndicator maxHeightVelocityArrow = Instantiate(arrowBasicFrontPivotPrefab).GetComponent <ArrowIndicator>(); maxHeightVelocityArrow.SetArrowTransform(cannonBall.PeakHightVelocityMarker); maxHeightVelocityArrow.bobbing = true; maxHeightVelocityArrow.Show(); vectorArrows.Add(maxHeightVelocityArrow); maxHeightVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true); energyWidget.AttachToTransform(cannonBall.EnergyWidgetMarker_LeftOffset); energyWidget.infoWidget.ToggleElasticInfo(false); energyWidget.infoWidget.ToggleKineticInfo(false); energyWidget.UpdateGravEnergy(cannonBall.Gravitational_Potential_Energy()); CanProcessNextStep = true; yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.SlopeDown); })); // wait for camera to complete lerping to Ball mid air position CanProcessNextStep = false; HideGizmoAndWidgets(); // Hide all force arrows in preperation for next view step distanceGizmo.Show(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform))); cannonBall.Resume(); // Cannonball falling downward while (!cannonBall.paused) { Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green); downMarker.position = cannonBall.transform.position; markers.Add(downMarker.gameObject); yield return(null); RaycastHit hitResult; if (RaycastFromPos(cannonBall.transform.position, -cannonBall.transform.up, Mathf.Infinity, out hitResult, true)) { float diff = cannonBall.transform.position.y - hitResult.point.y; if (diff < DistanceFromGround_At_TimeOFLaunch) { cannonBall.PauseInAir(); } } } yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform))); ArrowIndicator slopeDownVelocityArrow = Instantiate(arrowBasicBackPivotPrefab).GetComponent <ArrowIndicator>(); slopeDownVelocityArrow.bobbing = true; vectorArrows.Add(slopeDownVelocityArrow); OrientTransformToVector(ref cannonBall.ResultPoint_Centered, cannonBall.currentVelocity); slopeDownVelocityArrow.SetArrowTransform(cannonBall.ResultPoint_Centered); slopeDownVelocityArrow.Show(); slopeDownVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true); energyWidget.AttachToTransform(cannonBall.EnergyWidgetMarker_RightOffset); energyWidget.infoWidget.ToggleElasticInfo(false); energyWidget.UpdateGravEnergy(cannonBall.Gravitational_Potential_Energy()); energyWidget.Show(); CanProcessNextStep = true; yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.CompleteArc); })); // wait for camera to complete lerping to Ball mid air position CanProcessNextStep = false; HideGizmoAndWidgets(); distanceGizmo.Show(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform))); // RE focus camera cannonBall.Resume(); // resume cannon ball trajectory // Draw the rest of the arc markers while (!cannonBall.paused && cannonBall.inAir) { Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green); downMarker.position = cannonBall.transform.position; markers.Add(downMarker.gameObject); yield return(null); } cannonBall.transform.rotation = Quaternion.identity; midAirStep = MidAirStep.Undefined; // Mid air step is over; // Freeze the ball's movement and rotation as soon as it makes contact with the ground; cannonBall.transform.rotation = Quaternion.identity; cannonBall.rigidBody.constraints = RigidbodyConstraints.FreezeAll; // Cheat to make Cannonball touch ground cannonBall.transform.position = new Vector3(cannonBall.transform.position.x, 0.425f, cannonBall.transform.position.z); yield return(StartCoroutine(DoProcessStoppedState_Energy())); activeCoroutine = null; }
public IEnumerator DoProcessMidAirState_Forces() { sliderLockOut = true; // Ensure that we cannot manipulate physics attributes after launch Debug.Log("Launch Cannon Ball and Observe Forces while in air"); if (failLaunch) { popup.Show(); yield break; } HideGizmoAndWidgets(); distanceGizmo.Show(); CanProcessNextStep = false; midAirStep = MidAirStep.Launching; yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step3CamTransform))); float velocity = Velocity_At_Time_Of_Launch(); float ratio = ratio_Of_DEPE_Over_DGPE(); catapult.ThrowBall(catapult.launchVector.up, velocity); while (!cannonBall.paused) { yield return(null); Transform riseMarker = Utils.CreateMarker(null, 0.1f, Color.red); riseMarker.position = cannonBall.transform.position; markers.Add(riseMarker.gameObject); // Catch when the ball has left max arc height if (cannonBall.rigidBody.velocity.y < 0) { if (!cannonBall.maxHeightReached) { cannonBall.maxHeightReached = true; Transform peakMarker = Utils.CreateMarker(null, 0.2f, Color.blue); peakMarker.position = cannonBall.prevPosition; markers.Add(peakMarker.gameObject); cannonBall.PauseInAir(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform))); } } } //yield return new WaitUntil(() => { return !camLerping; }); // wait for camera to complete lerping to Ball mid air position midAirStep = MidAirStep.Maxheight; weightPoint = cannonBall.LeftWeightPoint; resultPoint = catapult.springVector; ArrowIndicator maxHeightVelocityArrow = Instantiate(arrowBasicFrontPivotPrefab).GetComponent <ArrowIndicator>(); maxHeightVelocityArrow.SetArrowTransform(cannonBall.PeakHightVelocityMarker); maxHeightVelocityArrow.bobbing = true; maxHeightVelocityArrow.Show(); vectorArrows.Add(maxHeightVelocityArrow); maxHeightVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true); gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint); gravArrow.Show(); CanProcessNextStep = true; yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.SlopeDown); })); // wait for camera to complete lerping to Ball mid air position CanProcessNextStep = false; HideGizmoAndWidgets(); // Hide all force arrows in preperation for next view step distanceGizmo.Show(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform))); cannonBall.Resume(); // Cannonball falling downward while (!cannonBall.paused) { Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green); downMarker.position = cannonBall.transform.position; markers.Add(downMarker.gameObject); yield return(null); RaycastHit hitResult; if (RaycastFromPos(cannonBall.transform.position, -cannonBall.transform.up, Mathf.Infinity, out hitResult, true)) { float diff = cannonBall.transform.position.y - hitResult.point.y; if (diff < DistanceFromGround_At_TimeOFLaunch) { cannonBall.PauseInAir(); } } } yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step4CamTransform))); gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint); gravArrow.Show(); ArrowIndicator slopeDownVelocityArrow = Instantiate(arrowBasicBackPivotPrefab).GetComponent <ArrowIndicator>(); slopeDownVelocityArrow.bobbing = true; vectorArrows.Add(slopeDownVelocityArrow); OrientTransformToVector(ref cannonBall.ResultPoint_Centered, cannonBall.currentVelocity); slopeDownVelocityArrow.SetArrowTransform(cannonBall.ResultPoint_Centered); slopeDownVelocityArrow.Show(); slopeDownVelocityArrow.infoWidget.UpdateInfoWidget(null, (float)Math.Round(cannonBall.currentVelocity.magnitude, 2), "Velocity", true); CanProcessNextStep = true; yield return(new WaitUntil(() => { return (midAirStep == MidAirStep.CompleteArc); })); // wait for camera to complete lerping to Ball mid air position CanProcessNextStep = false; HideGizmoAndWidgets(); distanceGizmo.Show(); yield return(StartCoroutine(DoLerpCameraToTransform(cannonBall.step5CamTransform))); // RE focus camera cannonBall.Resume(); // resume cannon ball trajectory // Draw the rest of the arc markers while (!cannonBall.paused && cannonBall.inAir) { Transform downMarker = Utils.CreateMarker(null, 0.1f, Color.green); downMarker.position = cannonBall.transform.position; markers.Add(downMarker.gameObject); yield return(null); } yield return(new WaitUntil(() => { return (cannonBall.rigidBody.velocity == Vector3.zero); })); // wait for camera to complete lerping to Ball mid air position CanProcessNextStep = true; cannonBall.transform.rotation = Quaternion.identity; midAirStep = MidAirStep.Undefined; // Mid air step is over; activeCoroutine = null; }
public IEnumerator DoProcessTimeOfLaunch_Forces() { CanProcessNextStep = false; yield return(StartCoroutine(DoLerpCameraToTransform(catapult.step2CamTransform))); Debug.Log("Observe Forces of Gravity, Spring and Resultant Force at time of launch"); catapult.rope.gameObject.SetActive(false); catapult.launchAngle = catapult.DEFAULT_LAUNCH_ANGLE; tensionPoint = cannonBall.TensionPoint; weightPoint = cannonBall.LeftWeightPoint; springPoint = cannonBall.SpringPoint_LeftOffset; resultPoint = cannonBall.ResultPoint_RightOffset; resultArrow.SetArrowTransform(resultPoint); resultArrow.Show(); ResultForce = 0; /** Smooth lerp the draining of tensions and adding of resultant force over constant time **/ const float drainSpeed = 0.035f; float initialTension = TensionForce; float lerpTension = TensionForce; while (!EEMath.Approximately(lerpTension, 0, 0.1f)) { lerpTension = Mathf.Lerp(lerpTension, 0, drainSpeed); TensionForce = lerpTension; ResultForce = initialTension - TensionForce; yield return(null); } yield return(new WaitForSeconds(2)); // Wait to process the changes of forces that was animated if (ResultForce <= 0) { popup.Show(); yield break; } tensionArrow.Hide(); springArrow.SetArrowTransform(catapult.springVector); gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint); springArrow.bobbing = gravArrow.bobbing = resultArrow.bobbing = false; catapult.throwCalled = true; failLaunch = false; float ratio = ratio_Of_DEPE_Over_DGPE(); if (ratio <= 1f) { catapult.launchAngle *= ratio; failLaunch = true; } while (catapult.throwCalled) { yield return(null); // Set all arrow starting positions Vector3 SVector = CalculateSVector(); springArrow.SetArrowTransform(catapult.springVector); catapult.springVector.transform.up = SVector.normalized; // Update Spring Force using KForce SpringK = springK; CalculateResultVector(); // calculate the result vector, as Spring resultArrow.SetArrowTransform(catapult.resultVector); gravArrow.SetArrowTransform(cannonBall.CenterWeightPoint); } springArrow.bobbing = gravArrow.bobbing = resultArrow.bobbing = true; springArrow.Show(); gravArrow.Show(); resultArrow.Show(); CanProcessNextStep = true; activeCoroutine = null; }