- Home /
Question by
dinobert10 · Jan 08, 2018 at 11:40 PM ·
dimensions
I want to make a game where you can switch from 2D 8 bit to 3D third person by going through a door. How would I code this?
Could I make two separate cameras and 2 areas for 8 bit and 3D?
Comment
Answer by KittenSnipes · Jan 09, 2018 at 10:02 PM
What have you tried so far? I played around a bit and at least figured out a hard part for you. The switch from 3d cam to 2d. So here you go:
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraProjectionChange : MonoBehaviour
{
public float ProjectionChangeTime = 0.5f;
public bool ChangeProjection = false;
private bool _changing = false;
private float _currentT = 0.0f;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
ChangeProjection = !ChangeProjection;
}
if (_changing)
{
ChangeProjection = false;
}
else if (ChangeProjection)
{
_changing = true;
_currentT = 0.0f;
}
}
private void LateUpdate()
{
if (!_changing)
{
return;
}
var currentlyOrthographic = GetComponent<Camera>().orthographic;
Matrix4x4 orthoMat, persMat;
if (currentlyOrthographic)
{
orthoMat = GetComponent<Camera>().projectionMatrix;
GetComponent<Camera>().orthographic = false;
GetComponent<Camera>().ResetProjectionMatrix();
persMat = GetComponent<Camera>().projectionMatrix;
}
else
{
persMat = GetComponent<Camera>().projectionMatrix;
GetComponent<Camera>().orthographic = true;
GetComponent<Camera>().ResetProjectionMatrix();
orthoMat = GetComponent<Camera>().projectionMatrix;
}
GetComponent<Camera>().orthographic = currentlyOrthographic;
_currentT += (Time.deltaTime / ProjectionChangeTime);
if (_currentT < 1.0f)
{
if (currentlyOrthographic)
{
GetComponent<Camera>().projectionMatrix = MatrixLerp(orthoMat, persMat, _currentT * _currentT);
}
else
{
GetComponent<Camera>().projectionMatrix = MatrixLerp(persMat, orthoMat, Mathf.Sqrt(_currentT));
}
}
else
{
_changing = false;
GetComponent<Camera>().orthographic = !currentlyOrthographic;
GetComponent<Camera>().ResetProjectionMatrix();
}
}
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;
}
}
After that I need a bit more info because this is a pretty vague question. I would like to know what you have thought up so far and what you are planning to do. It would help me help you more.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AddForce ignores X and Y components 0 Answers
How to teleport an object relative to itself 0 Answers
Change texture non-power of 2 setting at runtime? 1 Answer
Rendering specific layers inside sprite 0 Answers
Unity 4.6 UI Canvas width & height 2 Answers