// This function is responsible for moving the tokens and resetting the variables if there was a match, and putting them back into their previous positions // if there was not a match. public virtual void ExchangeTokens() { // Remember the starting and ending position for each token. Vector3 startPos = gameManager.GetWorldPositionFromGridPosition((int)exchangeGridPos1.x, (int)exchangeGridPos1.y); Vector3 endPos = gameManager.GetWorldPositionFromGridPosition((int)exchangeGridPos2.x, (int)exchangeGridPos2.y); // Get the new position for each token by lerping between startPos and endPos. Vector3 movePos1 = Vector3.Lerp(startPos, endPos, lerpPercent); Vector3 movePos2 = Vector3.Lerp(endPos, startPos, lerpPercent); // Update the position of each token object. exchangeToken1.transform.position = movePos1; exchangeToken2.transform.position = movePos2; // If both tokens have reached their final position: if (lerpPercent == 1) { // Tell the grid array the new positions of each token. gameManager.gridArray[(int)exchangeGridPos2.x, (int)exchangeGridPos2.y] = exchangeToken1; gameManager.gridArray[(int)exchangeGridPos1.x, (int)exchangeGridPos1.y] = exchangeToken2; Step--; StepText.gameObject.GetComponent <TextMesh> ().text = "Step: " + Step; // If this exchange has not created a match, move the tokens back. if (!matchManager.GridHasMatch() && userSwap) { SetupTokenExchange(exchangeToken1, exchangeGridPos2, exchangeToken2, exchangeGridPos1, false); // If this exchange has created a match, reset all variables and set movement to false. } else { exchangeToken1 = null; exchangeToken2 = null; move = false; } } }