banner



How To Use Multiple Camera To Display Same Screen In Unity

If you have worked on unity and been overusing the Main Camera and thinking why even go on the choice to add new cameras to the scene… Well, allow me to enlighten you… :P

Disclaimer: I assume yous accept off-white cognition of Unity basics (and Git)… if not yous can detect information technology hither… Come back shortly…

Ever heard of Portal…?

Yup.. that's the one…

Shall we try replicating it?

Aye you heard me… (Disclaimer yet over again.. I don't actually know the internal workings of the game… and am too lazy to look it upwards… Information technology'south just my take on it, and I don't expect information technology to even approach it's level… It takes fashion more than time and effort to build something that good, atleast more than than ane night… :P)

For this tutorial we will focus only on the visual aspects of the portal, i.e. seeing through them.

  • Clone the repository in your desired folder (*whispers* type the beneath commands in your terminal/command prompt)
            git clone git@github.com:Neelarghya/unity-multi-cam.git          
  • One time cloned in the right binder checkout the commit 576765c
    To go back in time to the betoken from where we tin can follow this tutorial
    The Repo contains the complete solution if you just desire that…
            cd unity-multi-cam
git checkout 576765c
  • You should be greeted with a Project similar:

Y'all will find a project with a maze (covered in bathroom tiles) like setup, with a First Person controller. At this point if you play the scene you would be able to motion and spring around in the environment.
If you lot notice advisedly you might also find 2 screen like images on the walls. 1 with a bluish border some other with the orange border.

These images are actually Globe Space Canvases with 2 child objects.
An Paradigm which just acts as the border to make information technology noticeable and
A Raw Image which will be the basis of our portals.

PortalCanvas Prefab

Let'south beginning Edifice Portals..!

  • Add two Render Textures to your Images in the Project Window… And lets phone call them Portal1Texture and Portal2Texture

  • Select the RawImages in each Portal Canvases and assign the respective RawTextures (Portal1Texture and Portal2Texture) to their texture field.
    This insures that the RawImages Display the texture that is fed into these RawTextures. One time done the Screens volition bear witness Orange and Bluish as no texture feed is present as of however.
    [Commit: 10d4765]

  • Permit's feed these textures… Add two Cameras and place them at the centres of the 2 canvases, such that they look out of the canvases, similar then…

  • Once you lot accept added both the cameras in the right positions, with the right rotations, Add the opposite RawTextures to the camera's target texture. i.e. PortaloneCamera gets Portal2Texture (RawTexture) and vice versa.

  • Once done, hitting play… you will run across the portals kind of looks like images taken from each other… Simply that'due south exactly what we have done…
    They tin can display your movement from each other's perspective but they fundamentally lack something… Any guesses?
    They are also apartment… and don't visually portray whatsoever depth.
    [Commit: bf4bd4a]
  • Allow's add some perspective, add a Scripts binder to your Assets and add a script called PerspectiveCamera to information technology. Assign the scripts to the 2 Portal cameras we have.
  • Remove any unnecessary code and just add together 2 Transform backdrop one for the Principal Camera (Which is assigned during start) and another for the Display screen. And a Vector3 Property to store the Initial direction of the camera. Don't forget to assign the Serialized variable display with the opposite displays. (Canvas2 for Camera1 and vice versa)
    [Commit: be23dc1]
            using UnityEngine;

public course PerspectiveCamera : MonoBehaviour
{
[SerializeField] individual Transform display;
private Transform _camera;
private Vector3 _initialForward;

individual void Start()
{
_camera = GameObject
.FindGameObjectWithTag("MainCamera")
.transform;

_initialForward = transform.forrard;
}
}

  • It'south time to add together some logic to information technology… Add an Update to the script
    This might look a flake convoluted at showtime, merely… When we go down to it…
    Nosotros demand to rotate the the camera of the Portal, based on from where the user is viewing the other Display.
            private void Update()
{
Vector3 axisOfRotation =
Vector3.Cross((display.position - _camera.position)
, -display.forrard)
.normalized;

float angleBetween =
Vector3.Angle( _camera.position - brandish.position,
-display.forrad);

transform.forward =
Quaternion.AngleAxis(angleBetween, axisOfRotation)
* _initialForward;
}

The management between the user and the brandish is (brandish.position — _camera.position) "P" as in histrion.
Allow the the initial forward direction be represented by "I"
And the Display'southward Forward management be "D" (Note that a sail' Forward is actually into the plane which is why we accept negations in the lawmaking)

As we observe the angle between P and D is the same equally the Angle betwixt the required view (the out going arrow in Portal2) and I.
Thus the new direction will exist the initial direction + the Bending between them (i.east. Theta in the prototype (medium doesn't provide special characters, pardon me))
This Angle needs an axis of rotation, that we get via a cross product betwixt the ii Vectors D and P
All in all nosotros get
New View Direction = Initial Direction + Theta
Evidently and Simple

[Commit: e9c1f40]

  • Finally you lot can add trigger colliders to teleport your player from one portal to another
    [Commit: 060b6e7]

There can be and so many more applications of multiple cameras… This was just 1 thought experiment… Hope that was fun… Until adjacent fourth dimension…

Source: https://medium.com/xrpractices/multiple-cameras-and-when-to-use-them-unity3d-c271cb9a1f02

Posted by: wrightequill.blogspot.com

0 Response to "How To Use Multiple Camera To Display Same Screen In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel