How to play a sequence of actions

I am building a 2D RPG game. In the turn-based battle scene, after the player clicks the Start button, the hero will first be translated to the enemy’s position, wait for a while and and then be moved back to his original position. I have problem playing these three sequences of actions.

I have a function that is called when user clicks the start button:

void OnStartButtonClick() {
    StartCoroutine(CharacterAttack());
}

IEnumerator CharacterAttack() {
        // store the player's original position
        Vector3 originalPosition = player.gameObject.transform.position; 

        // move player to enemy's position
        yield return StartCoroutine( MoveCharacterToPoint(player.gameObject, enemy.gameObject.transform.position) );
        yield return StartCoroutine( MoveCharacterToPoint(player.gameObject, originalPosition));
}

MoveCharacterToPoint is a function that does the fundamental translation.
However, the above code only moves the player to enemy’s position, but cannot move it back. How can I modify my code?

Use code tags.

Can you also post MoveCharacterToPoint()?

Here it is.

IEnumerator  MoveCharacterToPoint(GameObject character, Vector3  destination) {
    float timer = 0f;
    var StartPosition = character.transform.position;
    if (SpawnAnimationCurve.length > 0) {
        while (timer < SpawnAnimationCurve.keys[SpawnAnimationCurve.length - 1].time) {
              character.transform.position = Vector3.Lerp(StartPosition, destination, SpawnAnimationCurve.Evaluate(timer));
              timer += Time.deltaTime;
              yield return newWaitForEndOfFrame();
         }
     } else {
       character.transform.position = destination;
     }
}
2 Likes