/// <summary> /// Lerp Tile to position. /// The tile position in tilemap is only changed when the tile reaches the target. /// </summary> /// <param name="tilePosition"> Position of tile which we want move</param> /// <param name="endPosition"> Position where we want to place tile </param> /// <param name="timeOfMove"> How long tile should move</param> /// <returns></returns> public static IEnumerator LerpTileToPositionDiscrete(this Tilemap t, Vector3Int startPosition, Vector3Int endPosition, float timeOfMove) { float elapsedTime = 0; Vector3 startPos = VectorExtensionsUB.ConvertToVector3(startPosition); Vector3 endPos = VectorExtensionsUB.ConvertToVector3(endPosition); TileBase lerpedTile = t.GetTile <TileBase>(startPosition); float lerpTimeNormalized = 0; while (lerpTimeNormalized <= 1) { yield return(null); elapsedTime += Time.smoothDeltaTime; lerpTimeNormalized = elapsedTime / timeOfMove; Vector3 tempPosition = Vector3.Lerp(Vector3.zero, endPos - startPos, lerpTimeNormalized); t.SetTransformMatrix(startPosition, Matrix4x4.Translate(tempPosition)); } yield return(null); t.SetTile(endPosition, lerpedTile); t.SetTile(startPosition, null); }
/// <summary> /// Lerp Tile by offset /// The tile position in the tilemap is swapped immediately and then the movement from the initial position to the final one is performed. /// </summary> /// <param name="startPosition"> Position of tile which we want move</param> /// <param name="offset"> Offset about which we want move tile </param> /// <param name="timeOfMove"> How long tile should move</param> /// <returns></returns> public static IEnumerator LerpTileByOffsetContinuous(this Tilemap t, Vector3Int startPosition, Vector3Int offset, float timeOfMove) { float elapsedTime = 0; Vector3 endPosition = VectorExtensionsUB.ConvertToVector3(offset); TileBase lerpedTile = t.GetTile <TileBase>(startPosition); t.SetTile(startPosition + offset, lerpedTile); t.SetTile(startPosition, null); float lerpTimeNormalized = 0; while (lerpTimeNormalized <= 1) { elapsedTime += Time.smoothDeltaTime; lerpTimeNormalized = elapsedTime / timeOfMove; Vector3 tempPosition = Vector3.Lerp(endPosition, Vector3.zero, lerpTimeNormalized); t.SetTransformMatrix(startPosition, Matrix4x4.Translate(tempPosition)); yield return(null); } }