Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question A 2D platformer map to be repeating and having no borders

Discussion in '2D' started by The_wARmAcH1n3, Oct 3, 2023.

  1. The_wARmAcH1n3

    The_wARmAcH1n3

    Joined:
    Aug 5, 2023
    Posts:
    26
    I want a 2D platformer map to be repeating and having no borders. By that I mean that if I go past the right border, I come out on the left border. Same with top and bottom margins.

    I was thinking about copying the entire map to the border so that the map is always surrounded by the copies at the border. And then somehow using the gameobject.transform.position in the script to put the player and the enemies back in the middle every time they cross the border.

    Is there perhaps an easier way? The fact that the map appears a total of 9 times in the scene is a bit impractical.

    The map is larger than the screen, about 2-4 times the screen size, the camera follows the player. The opponents and the player should move freely within the level without level borders.

    The problem is, for example, if the player is to the left of the right border and the opponent is to the right of the right border, the opponent is not moved to the middle, but only when the player goes over the right border, then the player and the opponent should be in the middle area can be moved unnoticed (and come out at the left border) to create the feeling that the map can repeat itself infinitely if you always go in one direction.

    Like in this game:
     
  2. The_wARmAcH1n3

    The_wARmAcH1n3

    Joined:
    Aug 5, 2023
    Posts:
    26
    I got it to work, a script that does copy the map, attached to the tilemap. The position and the borders of the map I set manualy.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Tilemaps;
    4.  
    5. public class GroundTilemapScript : MonoBehaviour
    6. {
    7.     public GameObject tileMapPreflab;
    8.     protected Tilemap tileMapComponent;
    9.     protected Tilemap[] tileMapClone = new Tilemap[8];
    10.     public Vector2Int tileMapPosition;
    11.     public Vector2Int tileMapSize;
    12.     protected static bool hasDone = false;
    13.     public Grid grid;
    14.     public float tileMapBorderLeftPosition;
    15.     public float tileMapBorderRightPosition;
    16.     public float tileMapBorderTopPosition;
    17.     public float tileMapBorderBottomPosition;
    18.  
    19.     // Start is called before the first frame update
    20.  
    21.     void Awake()
    22.     {
    23.         tileMapBorderLeftPosition = (float)tileMapPosition.x - (((float)tileMapSize.x) / 2f);
    24.         tileMapBorderRightPosition = (float)tileMapPosition.x + (((float)tileMapSize.x) / 2f);
    25.         tileMapBorderTopPosition = (float)tileMapPosition.y + (((float)tileMapSize.y) / 2f); ;
    26.         tileMapBorderBottomPosition = (float)tileMapPosition.y - (((float)tileMapSize.y) / 2f); ;
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         if(!hasDone)
    32.         {
    33.             tileMapComponent = tileMapPreflab.GetComponent<Tilemap>();
    34.  
    35.             tileMapClone[0] = Tilemap.Instantiate(tileMapComponent);
    36.             tileMapClone[0].transform.SetParent(grid.gameObject.transform);
    37.             tileMapClone[0].transform.position = Vector2.left * tileMapSize.x + Vector2.up * tileMapSize.y;
    38.             tileMapClone[0].transform.position += tileMapComponent.transform.position;
    39.  
    40.             tileMapClone[1] = Tilemap.Instantiate(tileMapComponent);
    41.             tileMapClone[1].transform.SetParent(grid.gameObject.transform);
    42.             tileMapClone[1].transform.position = Vector2.left * tileMapSize.x;
    43.             tileMapClone[1].transform.position += tileMapComponent.transform.position;
    44.  
    45.             tileMapClone[2] = Tilemap.Instantiate(tileMapComponent);
    46.             tileMapClone[2].transform.SetParent(grid.gameObject.transform);
    47.             tileMapClone[2].transform.position = Vector2.left * tileMapSize.x + Vector2.down * tileMapSize.y;
    48.             tileMapClone[2].transform.position += tileMapComponent.transform.position;
    49.  
    50.             tileMapClone[3] = Tilemap.Instantiate(tileMapComponent);
    51.             tileMapClone[3].transform.SetParent(grid.gameObject.transform);
    52.             tileMapClone[3].transform.position = Vector2.up * tileMapSize.y;
    53.             tileMapClone[3].transform.position += tileMapComponent.transform.position;
    54.  
    55.             tileMapClone[4] = Tilemap.Instantiate(tileMapComponent);
    56.             tileMapClone[4].transform.SetParent(grid.gameObject.transform);
    57.             tileMapClone[4].transform.position = Vector2.down * tileMapSize.y;
    58.             tileMapClone[4].transform.position += tileMapComponent.transform.position;
    59.  
    60.             tileMapClone[5] = Tilemap.Instantiate(tileMapComponent);
    61.             tileMapClone[5].transform.SetParent(grid.gameObject.transform);
    62.             tileMapClone[5].transform.position = Vector2.right * tileMapSize.x + Vector2.up * tileMapSize.y;
    63.             tileMapClone[5].transform.position += tileMapComponent.transform.position;
    64.  
    65.             tileMapClone[6] = Tilemap.Instantiate(tileMapComponent);
    66.             tileMapClone[6].transform.SetParent(grid.gameObject.transform);
    67.             tileMapClone[6].transform.position = Vector2.right * tileMapSize.x;
    68.             tileMapClone[6].transform.position += tileMapComponent.transform.position;
    69.  
    70.             tileMapClone[7] = Tilemap.Instantiate(tileMapComponent);
    71.             tileMapClone[7].transform.SetParent(grid.gameObject.transform);
    72.             tileMapClone[7].transform.position = Vector2.right * tileMapSize.x + Vector2.down * tileMapSize.y;
    73.             tileMapClone[7].transform.position += tileMapComponent.transform.position;
    74.  
    75.             hasDone = true;
    76.         }
    77.     }
    78.  
    79.     private void OnDrawGizmosSelected()
    80.     {
    81.         Gizmos.color = Color.blue;
    82.         Gizmos.DrawWireCube((Vector2)tileMapPosition, (Vector2)tileMapSize);
    83.         Gizmos.DrawWireSphere((Vector2)tileMapPosition, 10f);
    84.     }
    85.  
    86. }
    87.  
    The script attached to the gameObjects (Enemys, Bullets and other). It calculates the shortest distance to the player of the possible transport positions and use this position for transport:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CheckObjectPositionScript : MonoBehaviour
    4. {
    5.     protected GameObject player;
    6.     protected SupanthaPaul.PlayerController playerCheck;
    7.     private Vector3 objectPosition;
    8.     private Vector3 playerPosition;
    9.  
    10.     // Start is called before the first frame update
    11.     protected virtual void Start()
    12.     {
    13.         player = FindAnyObjectByType<SupanthaPaul.PlayerController>().gameObject;
    14.         playerCheck = player.GetComponent<SupanthaPaul.PlayerController>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     protected virtual void FixedUpdate()
    19.     {
    20.         bool change = false;
    21.         objectPosition = transform.position;
    22.         playerPosition = player.transform.position;
    23.  
    24.         float distanceObjectX = Mathf.Abs(objectPosition.x - playerPosition.x);
    25.         float distanceObjectXRight = Mathf.Abs((objectPosition.x + playerCheck.tileMapSize.x) - playerPosition.x);
    26.         float distanceObjectXLeft = Mathf.Abs((objectPosition.x - playerCheck.tileMapSize.x) - playerPosition.x);
    27.         float distanceObjectY = Mathf.Abs(objectPosition.y - playerPosition.y);
    28.         float distanceObjectYTop = Mathf.Abs((objectPosition.y + playerCheck.tileMapSize.y) - playerPosition.y);
    29.         float distanceObjectYBottom = Mathf.Abs((objectPosition.y - playerCheck.tileMapSize.y) - playerPosition.y);
    30.  
    31.         if (distanceObjectX < distanceObjectXRight && distanceObjectX < distanceObjectXLeft)
    32.         {
    33.         }
    34.         else if (distanceObjectXRight < distanceObjectXLeft)
    35.         {
    36.             change = true;
    37.             objectPosition.x += (float)playerCheck.tileMapSize.x;
    38.         }
    39.         else
    40.         {
    41.             change = true;
    42.             objectPosition.x -= (float)playerCheck.tileMapSize.x;
    43.         }
    44.  
    45.         if (distanceObjectY < distanceObjectYTop && distanceObjectY < distanceObjectYBottom)
    46.         {
    47.         }
    48.         else if (distanceObjectYTop < distanceObjectYBottom)
    49.         {
    50.             change = true;
    51.             objectPosition.y += (float)playerCheck.tileMapSize.y;
    52.         }
    53.         else
    54.         {
    55.             change = true;
    56.             objectPosition.y -= (float)playerCheck.tileMapSize.y;
    57.         }
    58.         transform.position = objectPosition;
    59.  
    60.         if (change)
    61.             Debug.Log(gameObject.name +"    ");
    62.  
    63.  
    64.     }
    65. }
    66.  
    The script attached to the player:
    Code (CSharp):
    1.             playerPosition = transform.position;
    2.             if (playerPosition.x > tileMapBorderRightPosition)
    3.             {
    4.                 playerPosition.x -= (float)tileMapSize.x;
    5.             }
    6.             else if (playerPosition.x < tileMapBorderLeftPosition)
    7.             {
    8.                 playerPosition.x += (float)tileMapSize.x;
    9.             }
    10.             if (playerPosition.y > tileMapBorderTopPosition)
    11.             {
    12.                 playerPosition.y -= (float)tileMapSize.y;
    13.             }
    14.             else if (playerPosition.y < tileMapBorderBottomPosition)
    15.             {
    16.                 playerPosition.y += (float)tileMapSize.y;
    17.             }
    18.             transform.position = playerPosition;
    19.  
     
    Last edited: Oct 8, 2023