Over the weekend and the past few days I have been stuck getting this feature to work in my game.
GOAL: When the player reaches a score, bool levelUp, is true. I would like to move the ship gameobject up the Y Axis off the screen, POINT A to POINT B (Offscreen), then reposition just off screen at the bottom, then move the ship up the Y Axis back to POSITION A. Player state is then set to playing and the game continues.
I have NOT accomplished and the results have been unacceptable. Close. I thought I had it working well, but realized it was only working after I fired a projectile detected the collision, exploded the object, THEN the ship would come to rest in the proper place, POINT A.
I have used coroutines, Vector3.movetowards, for(…) with if statements in all of them and they have all worked the same way. I simply need help.
The ships behavior flies up, repositions itself at the bottom/off screen, flies across the screen and off, repositions at bottom/off screen etc, until I have an, for lack of better words, an UPDATE or interruption when I destroy another gameobject with a projectile. At times I had bool variables being switched true/false all over the place. (Desperation, :neutral: ) Anyways, I would like to solve this any efficient way I can with your help. And learn something.
This was one of many ways that yielded the same result. I could post all my flailings, but I’ll spare you the tedium. May be good for a laugh. After this is solved.
THANK YOU for taking your time to look at the code below and suggest other methods for accomplishing the goal above.
void Update ()
{
...
CheckScoreLevelUp(Score); //Function to check score and whether to LEVELUP
if(LevelUp)
{
StartCoroutine(LevelUpShipFlyUp());
StartCoroutine(LevelUpShipRepoOntoScreen());
}
}
IEnumerator LevelUpShipFlyUp()
{
if(LevelUp)
{
state = State.Invincible;
if (transform.position.y < levelThreshold) //Change levelThreshold to another less than POINT A
{
float amtToMove = shipMoveOnToScreenSpeed * Time.deltaTime;
Booster.SetActive(true);
transform.position = new Vector3(transform.position.x, transform.position.y + amtToMove,
transform.position.z);
if(transform.position.y >= 6.5)
{
transform.position = new Vector3(0f, -11.0f, transform.position.z); //REPOSITION
levelThreshold = -2.4f;
Booster.SetActive(false);
state = State.Playing;
yield break;
}
Debug.Log ("SHIP FLY UP");
}
}
}
void CheckScoreLevelUp(int Score)
{
Debug.Log ("SCORE: " + Score);
switch(Score)
{
case 500:
LevelUp = true;
break;
case 1000:
LevelUp = true;
break;
case 2000:
LevelUp = true;
break;
}
}
IEnumerator LevelUpShipRepoOntoScreen()
{
while(transform.position.y < -2.40)
{
// MOVE SHIP INTO PLAY AREA (UP)
float amtToMove = shipMoveOnToScreenSpeed * Time.deltaTime;
transform.position = new Vector3(0f, transform.position.y + amtToMove, transform.position.z);
yield return 0;
}
}
