Home > Back-end >  switch from 3D to 2D with a button
switch from 3D to 2D with a button

Time:06-04

I want to make a 3D game, but when I press a button in this game, I want it to switch to a 2D image. Is it possible to trigger this by pressing a key with code, just like we clicked on the 2D button in unity?

CodePudding user response:

There is, in and of itself, no difference between 2D and 3D "view" in Unity. Everything in Unity exists in a 3D space at all times, so there is no real switching. However there are some tricks you could do to make it look like you are switching between those two.

What you could do is

  • Change the camera angle so everything is seen a different angle instead of the only from the front
  • Replace all your 2D sprites with equivalent 3D objects (Replacing a square with a cube etc), or simply having everything as 3D objects from the start.

CodePudding user response:

The main difference between a 2D and a 3D scene is in the camera projection, usually in 2D projects the camera is orthographic.

enter image description here

enter image description here

Now to solve this question, you have to switch the camera between prespective and orthographic modes. This process begins with saving the camera matrices.

private Camera cam;

private Matrix4x4 project2D;
private Matrix4x4 project3D;

public void Start()
{
    cam = GetComponent<Camera>();

    var temp = cam.orthographic;
    
    cam.orthographic = true;
    project2D = cam.projectionMatrix;
    cam.orthographic = false;
    project3D = cam.projectionMatrix;

    cam.orthographic = temp;
}

In the above code we saved the camera matrix information in these two modes, now you need to create a way to smoothly switch between the two modes with the following code. This script is called MatrixLerp and it performs this task.

private Matrix4x4 MatrixLerp(Matrix4x4 from, Matrix4x4 to, float t)
{
    t = Mathf.Clamp(t, 0.0f, 1.0f);
    var newMatrix = new Matrix4x4();
    newMatrix.SetRow(0, Vector4.Lerp(from.GetRow(0), to.GetRow(0), t));
    newMatrix.SetRow(1, Vector4.Lerp(from.GetRow(1), to.GetRow(1), t));
    newMatrix.SetRow(2, Vector4.Lerp(from.GetRow(2), to.GetRow(2), t));
    newMatrix.SetRow(3, Vector4.Lerp(from.GetRow(3), to.GetRow(3), t));
    return newMatrix;
}

In the next step, this IEnumerator will change the projection modes by changing the progress value. In the following Update() codes, you can switch between the two modes by pressing keys 1 and 2. Just insert the switch methods into any other code you want to solve this problem.

public float projectTime = 1f;
private IEnumerator SwitchProjection(Matrix4x4 projectTo)
{
    var progress = 0f;

    var currentProject = cam.projectionMatrix;
    
    while (progress < 1)
    {
        progress  = Time.deltaTime/projectTime;
        
        cam.projectionMatrix = MatrixLerp(currentProject, projectTo, progress);

        yield return null;
    }

    cam.orthographic = projectTo == project2D;
}
void Update()
{
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        StartCoroutine(SwitchProjection(project3D));
    }
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        StartCoroutine(SwitchProjection(project2D));
    }
}
  • Related