- Home /
Screen is 2 cameras with each different properties.
Hello everybody!
First of all, Merry Christmas!
And now to the point : I want to make a game (yeah Cliché beginning) and I want the main character (1ST-Person camera) to be split in 2. I mean it in a sense of left eye right eye. So with the left eye you see like a human (it's a horror game) and the right like a camera. I want each eye to see different things. Like in the left part of the screen I see ghosts, while on the right part I do not see them.
I know I can make the screen split in two with Screen.width/2. But now the question. How should I make it? I mean the 2 camera's. Make 2 Camera's and give them each another tag? I honestly do not know how to begin, yet I am all fired up with coal from Santa, and I want to do it. Both the eyes have to be controlled by player 1, so it's not going to be a multiplayer game. Don't mind the controls, I can get that done. The problem is with the camera's.
And for that I need your help.
So, thanks in advance, and have a nice day! Daniel Nowak Janssen
Try using layers and by assigning culling mask(number of layers each camera sees) to each camera. Select camera ->camera component->culling mask->select the layer of the game objects for which this camera should see. Hope this may help you at a very basic level Nsks
Answer by LazyElephant · Dec 15, 2015 at 05:46 AM
What you will need is called a scissor rect as talked about in this other question http://answers.unity3d.com/questions/134413/how-do-i-render-only-a-part-of-the-cameras-view.html
In a test scene, the steps I followed to get this working were:
Create a new layer to hold your invisible objects. (Edit->Project Settings->Tags and Layers)
Create an empty game object to use as a parent for your two cameras, this will allow you to move them around as if they were one.
Move the default camera in the scene to be a child of the empty game object. Make sure that the transform of the camera is at (0, 0, 0). In the inspector, click on the Culling Mask and uncheck the layer you want your invisible objects to be on. Make sure the Depth is set to 0.
Add a 2nd camera to the scene and make it a child of the empty game object. make sure the transform is the same as the first camera. In the inspector, make sure the Culling Mask is Everything and set the Depth to 1. Remove the Audio Listener component from this camera or unity will give you errors about having 2 in the scene.
Create a new c# script and name it Scissor. Copy and paste the code below into it. Note that this is the same code as in the question I linked, but changed a little because it wasn't running in Unity 5 since they removed the camera shortcut in favor of using GetComponent.
using UnityEngine; using System.Collections; public class Scissor : MonoBehaviour { public Rect scissorRect = new Rect (0,0,1,1); private Camera _camera; void Awake(){ _camera = GetComponent<Camera>(); } public static void SetScissorRect( Camera cam, Rect r ) { if ( r.x < 0 ) { r.width += r.x; r.x = 0; } if ( r.y < 0 ) { r.height += r.y; r.y = 0; } r.width = Mathf.Min( 1 - r.x, r.width ); r.height = Mathf.Min( 1 - r.y, r.height ); cam.rect = new Rect (0,0,1,1); cam.ResetProjectionMatrix (); Matrix4x4 m = cam.projectionMatrix; cam.rect = r; Matrix4x4 m1 = Matrix4x4.TRS( new Vector3( r.x, r.y, 0 ), Quaternion.identity, new Vector3( r.width, r.height, 1 ) ); Matrix4x4 m2 = Matrix4x4.TRS (new Vector3 ( ( 1/r.width - 1), ( 1/r.height - 1 ), 0), Quaternion.identity, new Vector3 (1/r.width, 1/r.height, 1)); Matrix4x4 m3 = Matrix4x4.TRS( new Vector3( -r.x * 2 / r.width, -r.y * 2 / r.height, 0 ), Quaternion.identity, Vector3.one ); cam.projectionMatrix = m3 * m2 * m; } // Update is called once per frame void OnPreRender () { SetScissorRect( _camera, scissorRect ); } }
Add the new script to your 2nd camera. In the inspector, change the Scissor Rect of the script to w=0.5 and h=1. This will set the view port to the left half of the screen.
The only other thing you have to remember to do is put all the objects that are invisible on the special layer you created. They should only be displayed in the left half of the screen. Hope this helps.
First of all thank you!
Second of all, What does matrix do in this context? Does it do the arbitrary linear 3D transformations? Can you maybe explain to me what that $$anonymous$$atrix 4x4 does?
Thank you again for your time, And have a $$anonymous$$erry Christmas, $$anonymous$$ Nowak Janssen
I don't understand the details either, but I can give a basic explanation. In Unity, if you resize the camera's view port, it doesn't just cut out the parts of the screen that would be outside the new size, it shrinks the scene down a bit. For instance, if you have a cube in the direct center of the screen and change the camera width to .5, you'll still see all of the cube in the screen, still in the middle of the view port, although smaller. You can test this yourself in the editor.
The three matrices made with the $$anonymous$$atrix4x4.TRS (Translate, Rotate, Scale) change the view port of the camera to match what you would see on just the portion of the screen you want as if the camera view port were still full size.
Somehow, my second camera does not show only the layer I want. I made a new layer called Ghost, and I made one enemy in that layer. But both my camera's see the same enemy, but only the left part of the screen should see the enemy. $$anonymous$$aybe I did something wrong? I do not know, but maybe it is conflicting with my $$anonymous$$ouseLook script. (It makes you move your camera. Here is the script :
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
public enum RotationAxes
{
$$anonymous$$ouseXAndY = 0,
$$anonymous$$ouseX= 1,
$$anonymous$$ouseY= 2
}
public RotationAxes axes = RotationAxes.$$anonymous$$ouseXAndY;
public float SensitivityHor = 9.0f;
public float SensitivityVer = 9.0f;
public float $$anonymous$$imumVert = -45.0f;
public float maximumVert = 45.0f;
private float _rotationX = 0;
// Update is called once per frame
void Start()
{
Rigidbody body = GetComponent<Rigidbody>();
if (body != null)
body.freezeRotation = true;
}
void Update () {
if ( axes == RotationAxes.$$anonymous$$ouseX)
{
//horizontal movement
transform.Rotate(0,Input.GetAxis("$$anonymous$$ouse X") * SensitivityHor, 0);
}
else if (axes == RotationAxes.$$anonymous$$ouseY)
{
//vertical movement
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * SensitivityVer;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert);
float rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
else
{
//makes it possible to rotate my head
_rotationX -= Input.GetAxis("$$anonymous$$ouse Y") * SensitivityVer;
_rotationX = $$anonymous$$athf.Clamp(_rotationX, $$anonymous$$imumVert, maximumVert);
float delta = Input.GetAxis("$$anonymous$$ouse X") * SensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
I made 2 Camera's and made them children to my $$anonymous$$ain character (a capsule for testing) and still the enemy does not disappear on the right side of my screen. it just stays visible although it should not.
Thanks again for your help, and have a $$anonymous$$erry Christmas. $$anonymous$$ Nowak Janssen.
Your answer
Follow this Question
Related Questions
How can i make the camera look up and down through keyboard keys? 2 Answers
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers
Unity - Dancing Ball World camera following and rotating system 1 Answer
Would anyone teach me how to create camera movement and look controls? 2 Answers
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer