Vector3 Lerp for Chess Like Game

Hi

I have been making a chees like game in Unity and have gathered some scripts to move a piece. Here is the code below:

It is moving the Selected piece perfectly to the position but I am battling in getting it to Lerp there. I dont know if I am making much sense but it moves directly to the position and I want it to move slowly there.

Any ideas on how to modify the code?

Thanks in Advance :smiley:

public void MovePiece(Vector2 _coordToMove)
	{
		bool validMovementBool = false;
		Vector2 _coordPiece = new Vector2(SelectedPiece.transform.position.x, SelectedPiece.transform.position.z);
		
		// Don't move if the user clicked on its own cube or if there is a piece on the cube
		if((_coordToMove.x != _coordPiece.x || _coordToMove.y != _coordPiece.y) || _boardPieces[(int)_coordToMove.x,(int)_coordToMove.y] != 0)
		{
			validMovementBool	= TestMovement (SelectedPiece, _coordToMove);
		}
		
		if(validMovementBool)
		{
			_boardPieces[(int)_coordToMove.x, (int)_coordToMove.y] = _boardPieces[(int)_coordPiece.x, (int)_coordPiece.y];
			_boardPieces[(int)_coordPiece.x , (int)_coordPiece.y ] = 0;
			
			SelectedPiece.transform.position = new Vector3 (_coordToMove.x, _pieceHeight, _coordToMove.y);		// Move the piece
		
			ChangeState (2);

			AttackPiece ();
		}
	}

Hereโ€™s an example of how to use a coroutine to lerp something overtime outside of Update, you should be able to quite easily apply the concept to what you want to do!

public Transform t;
float duration = 1f;

void Update () {
	if(Input.GetKeyDown(KeyCode.Q)){
		StartCoroutine(MyMethod());
	}
}

IEnumerator MyMethod(){
	float startTime = Time.time;
	Vector3 startPos = t.position;
	Vector3 endPos = startPos + Vector3.up;
	float progress = 0;
	
	while(progress < 1){
		t.position = Vector3.Lerp(startPos, endPos, progress);
		progress = (Time.time-startTime)/duration;
		yield return null;
	}
	t.position = endPos;
	Debug.Log("done");
}

Hope that helps,

Scribe!