Cinemachine 2D Level Wrap

Hello,

I’m new to Unity and I’m trying to create a 2D shoot ’em up game. I got one level and I want to create a seamless horizontal wrap effect. I decided to implement the effect with 2 duplications of my level, each on one side. I created the sprites so that they merge seamlessly into each other.

For the camera movement I’m using cinemachine. My virtual camera is following my player. Futhermore I added a confiner to the virtual camera to keep the camera record my level. The bounding shape of the confiner is set to the whole level included the duplicates on the sides.

On the ends of my main level instance, I placed two 2D BoxColliders. When the Player enters a BoxCollider it is teleported to the opposite side.
Here is my script, which is added to the colliders:

using Cinemachine;
using UnityEngine;

public class WorldWrapper : MonoBehaviour
{
    public GameObject player;
    public CinemachineVirtualCamera vCam1;

    private CinemachineComponentBase _myCamera;
    private void Start()
    {
        _myCamera = vCam1.GetCinemachineComponent<CinemachineComponentBase>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Vector3 playerPos = player.transform.position;
        Vector3 camPos = vCam1.transform.position;
        
        if (player)
        {
            player.transform.position = new Vector3(-playerPos.x, playerPos.y, playerPos.z);
            Vector3 positionDelta = new Vector3(-camPos.x - camPos.x, 0, 0);
            _myCamera.OnTargetObjectWarped(player.transform, positionDelta);
        }
    }

This is working but, I got a problem with the camera positioning on players teleport. After the the player is teleported, the camera is not showing the exact picture of the level as before. The camera is a little bit offset, that’s why the wrap is noticeable.

Currently I have no idea how to fix the camera position so the wrap isn’t noticeable. I would be pleased about solution suggestions.


Update

I think I figured out my problem. It’s not a probelm of the camera or of the script. It is a error in my reasoning. The teleport collider does not have to teleport the player to the opposite side of the main level. It has to teleport to player to the end of the opposite level duplication, which is adjacent to the main level.

For better unterstanding I created a sketch of my problem:

I think this will cause a problem with the current positions of my teleport colliders. What is the best way to solve this problem?

Edit:
That makes sense. Reposition your colliders to the correct spots. Or if you have an actual strip of levels like that, maybe create a gap between them for the collider to reside in.


For the script to work correctly if your camera and player do not have the exact same X coordinate —
positionDelta is meant to be the target’s position change. Interface ICinemachineCamera | Package Manager UI website

Try:

 using Cinemachine;
 using UnityEngine;
 
 public class WorldWrapper : MonoBehaviour
 {
     public GameObject player;
     public CinemachineVirtualCamera vCam1;
 
     private CinemachineComponentBase _myCamera;
     private void Start()
     {
         _myCamera = vCam1.GetCinemachineComponent<CinemachineComponentBase>();
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         Vector3 playerPos = player.transform.position;
         Vector3 camPos = vCam1.transform.position;
         
         if (player)
         {
             player.transform.position = new Vector3(-playerPos.x, playerPos.y, playerPos.z);
             Vector3 positionDelta = new Vector3(-playerPos.x - playerPos.x, 0, 0);
             _myCamera.OnTargetObjectWarped(player.transform, positionDelta);
         }
     }

Here’s what I’m seeing in my head: