void Update() { //States based on key-presses if (Input.GetKeyDown(KeyCode.D)) { if (currentState == falconStates.gliding) { previousState = falconStates.gliding; } else if (currentState == falconStates.boosting) { previousState = falconStates.boosting; } else if (currentState == falconStates.transition) { previousState = falconStates.transition; } currentState = falconStates.diving; } else if (Input.GetKeyUp(KeyCode.D)) { if (currentState == falconStates.diving) { currentState = falconStates.transition; } } /***********GLIDE STATE****************/ if (currentState == falconStates.gliding) { timeGlide += Time.deltaTime; currentYVelocity = glidingYEquation(timeGlide); currentXVelocity = glidingXEquation(timeGlide); lastGlideYVelocity = currentYVelocity; lastGlideXVelocity = currentXVelocity; timeDive = 0.0f; //Reset Dive Time timeBoost = 0.0f; //Reset Boost Time movementVect = new Vector2(currentXVelocity, -currentYVelocity); } /***********TRANSITION STATE****************/ else if (currentState == falconStates.transition) { timeTransition += Time.deltaTime; //If transition over, return to boost if (timeTransition >= diveToBoostTransitionTime) { currentState = falconStates.boosting; boostYEnergyAfterTransition = boostEnergy; boostXEnergyAfterTransition = boostEnergy; timeBoost = 0.0f; } currentYVelocity = diveTransitionYEquation(timeTransition); currentXVelocity = diveTransitionXEquation(timeTransition); lastTransitionXVelocity = currentXVelocity; lastTransitionYVelocity = currentYVelocity; boostEnergy -= Time.deltaTime; movementVect = new Vector2(currentXVelocity, -currentYVelocity); } /************BOOST STATE**************/ else if (currentState == falconStates.boosting) { timeBoost += Time.deltaTime; //If no more boost energy, return to gliding if (boostEnergy <= 0) { currentState = falconStates.gliding; } //Boosting else { currentYVelocity = -sinRiseYEquation(timeBoost); currentXVelocity = sinRiseXEquation(timeBoost); lastBoostYVelocity = currentYVelocity; lastBoostXVelocity = currentXVelocity; } boostEnergy -= Time.deltaTime; //Subtract from boost energy every second timeDive = 0.0f; //Reset dive time timeGlide = 0.0f; //Reset glide time timeTransition = 0.0f; //Reset transition time movementVect = new Vector2(currentXVelocity, -currentYVelocity); } /**************DIVE STATE**************/ else if (currentState == falconStates.diving) { timeDive += Time.deltaTime; boostEnergy = boostEnergyMultiplier * timeDive; //Convert time diving into boost power boostEnergyTotal = boostEnergy; //Stores the maximum boost energy gained from dive initialBoostX = timeDive * boostXScale; timeGlide = 0.0f; //Reset glide counter to 0 timeBoost = 0.0f; //Reset boost counter to 0 timeTransition = 0.0f; //Reset transition counter to 0 diveToBoostTransitionDone = false; //Reset transition toggle diveToBoostTransitionTime = boostEnergyTotal / transitionFactor; //What portion of the boost should be transition currentYVelocity = divingYEquation(timeDive); currentXVelocity = divingXEquation(timeDive); lastDiveYVelocity = currentYVelocity; lastDiveXVelocity = currentXVelocity; movementVect = new Vector2(currentXVelocity, -currentYVelocity); } Debug.Log(movementVect); Debug.Log(currentState); transform.Translate(movementVect * Time.deltaTime, Space.World); }
void Update() { downKeyDown = Input.GetKeyDown(keyForMovement); downKeyPressed = Input.GetKey(keyForMovement); downKeyReleased = Input.GetKeyUp(keyForMovement); //if you pressed down, record the y position if (downKeyDown) { //DisplayDebugLine(); } //if youre holding down, youre diving. if (downKeyPressed) { CurState = falconStates.diving; } //If you released, you are in release else if (downKeyReleased) { CurState = falconStates.release; } switch (CurState) { case falconStates.diving: DivingControl(); break; case falconStates.release: ReleaseControl(); break; case falconStates.ascending: AscendingControl(); break; case falconStates.gliding: GlidingControl(); break; } ApplyXDrag(); }
/******************************************** *****************END EQUATIONS*************** ********************************************/ void Start() { currentState = falconStates.gliding; currentYVelocity = 0; currentXVelocity = minXSpeed; lastBoostXVelocity = minXSpeed; movementVect = new Vector2(currentXVelocity, currentYVelocity); }
/// <summary> /// Release control /// track the y velocity you're at, and give yourself an x boost based on that velocity /// go to ascending state and set the amount of time you'll be there /// </summary> void ReleaseControl() { yVelEndDive = rb.velocity.y; rb.AddForce(new Vector2(-rb.velocity.y, 0) * movementSpeed, ForceMode2D.Force); timeInAscend = timeInAscendMult; CurState = falconStates.ascending; }
/// <summary> /// Ascending Control /// add force equal to a multiple of yVelEndDive (the velocity you were at when you released) to your y /// if you run out of time, go back to gliding /// </summary> void AscendingControl() { timeInAscend -= Time.deltaTime; rb.AddForce(new Vector2(0, - yVelEndDive) * movementSpeed * ascendVelMult * Time.deltaTime, ForceMode2D.Force); if (timeInAscend <= 0) { timeInAscend = 0; CurState = falconStates.gliding; } }
//add force of your transform.right to ONLY your y, slerp the angle based on state in ascending, if you're higher than maxRise, go back to gliding void AscendingControl() { float rotationSpeed = 0; float angle = 0; switch(ascendState) { case ascendingStates.ascendingInit: rotationSpeed = initUpRotationSpeed; angle = ascendInitAngle; if (rb.velocity.y < 0) { rb.AddForce(new Vector2(0, ascendInitVerticalMultiplier * rb.velocity.y * -1)); } break; case ascendingStates.ascending: rotationSpeed = upRotationSpeed; angle = ascendAngle; float yRatio = (yPosDiveStart - transform.position.y) / (yPosDiveStart - yPosDiveEnd); yRatio = yRatio / 2; rb.AddForce(new Vector2(0, yRatio) * movementSpeed, ForceMode2D.Force); break; } transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, angle), Time.deltaTime * rotationSpeed); if (transform.position.y > yOfMaxRise) { CurState = falconStates.gliding; } }
// Update is called once per frame void Update() { downKeyDown = Input.GetKeyDown(keyForMovement); downKeyPressed = Input.GetKey(keyForMovement); downKeyReleased = Input.GetKeyUp(keyForMovement); //if you pressed down, record the y position if (downKeyDown) { yPosDiveStart = transform.position.y; DisplayDebugLine(); } //if youre holding down, youre diving. If you released, you are in release if (downKeyPressed) { CurState = falconStates.diving; } else if (downKeyReleased) { CurState = falconStates.release; } switch(CurState) { case falconStates.diving: DivingControl(); break; case falconStates.release: ReleaseControl(); break; case falconStates.ascending: AscendingControl(); break; case falconStates.gliding: GlidingControl(); break; } //Control max x, min x, max y velocities if (rb.velocity.x > maxSpeedX) { rb.velocity = new Vector2(maxSpeedX, rb.velocity.y); } if (rb.velocity.x < minSpeedX) { rb.velocity = new Vector2(minSpeedX, rb.velocity.y); } if (rb.velocity.x > xSpeedDefault) { rb.AddForce(new Vector2(xDrag, 0)); } if (rb.velocity.y > maxSpeedY) { rb.velocity = new Vector2(rb.velocity.x, maxSpeedY); } }
//track the max ascending angle, track the y position at which you should stop ascending, start the ascend init timer void ReleaseControl() { rb.AddForce(new Vector2(-rb.velocity.y, 0) * movementSpeed * releaseBoostMultiplier, ForceMode2D.Force); ascendAngle = (yPosDiveStart - transform.position.y) * angleMult; if (ascendAngle < ascendInitAngle) { ascendAngle = ascendInitAngle; } if (ascendAngle > ascendAngleMax) { ascendAngle = ascendAngleMax; } yPosDiveEnd = transform.position.y; yOfMaxRise = yPosDiveStart + (yPosDiveStart - yPosDiveEnd) * yAscendMult; CurState = falconStates.ascending; StartCoroutine(initAscendTimer()); }