Visual rendering problem with 2 cameras (Perspective + Orthographic)

Hello friends,

I have a small problem with my project.
I’m trying to make a beat them up in 2.5D.
In my scene I have a Terrain object that serves as the ground, and my main character “Player” who is in 2D pixel art.

Originally I was only using one camera for both but in Projection: Perspective, my sprite render was distorted.

The solution I found was to use one camera (Main Camera) in Projection: Perspective, with Culling Mask which displays everything except my Player character and Depth: 0 and Clear Flags: Solid Color. It has an X axis rotation of 25. I use Cinemachine FreeLook Camera with Follow: Player and Look at: Player.

For my Player I use a camera (Camera Player) in Orthographic, Clear Flags: Depth only, Depth: 1, Culling Mask: Player, I also use Cinemachine 2D Camera with Follow: Player and Look at: Player. And with Pixel Perfect Camera component.

I also have a script to manage the position of the cameras:

using UnityEngine;

public class CameraAligner : MonoBehaviour
{
    public Camera mainCamera3D; 
    public Camera camera2D;    
    public Vector3 positionOffset; 
    public Vector3 rotationOffset; 

    void Update()
    {
        camera2D.transform.position = mainCamera3D.transform.position + positionOffset;

        camera2D.transform.rotation = mainCamera3D.transform.rotation * Quaternion.Euler(rotationOffset);

        if (camera2D.orthographic)
        {
            camera2D.orthographicSize = mainCamera3D.orthographicSize;
        }
    }
}

With this, the scene seems to display well, I have my 3D Terrain which serves as a ground and my 2D character whose pixel display is correct.

But in reality there’s a problem between the two cameras, for example:

  • If my character moves upwards, he falls into the void before reaching the edge of the Terrain.

  • If I add a 3D Cube to the Terrain, my character passes through it, but slightly lower down the Cube, my character is blocked as if the Cube were actually lower down.

There’s a discrepancy between the visual rendering which seems to be due to the fact that one camera (Main Camera) which handles 3D is in Perspective whereas Camera Player which handles 2D is in Orthographic.

Is it possible to deal with this problem or do I need to rethink my logic?

Thank you

For the record, I had forgotten to link my script that manages the cameras to a game object. Once I’d done that and played around with the Position Offset, the result wasn’t too bad. Although I doubt it’s a very good practice.