phunder
phunder

Reputation: 1703

Why does my camera not show items unless I move it to a negative Z position in 2D

Good day all

I am working on a basic 2D game to get the hang of using Unity. I wrote a function that allows me to constrain how far several game objects are allowed to move that I use for both the camera as well as my player character and my enemy units. The function uses Mathf.clamp to limit this movement.

My problem came in when I applied the function to my camera. Initially I was using a Vector2 to define the position constraints. This however caused all of the game elements to disappear.

After some good advice from a previous post I made (see here)(also, please don't tag this as a duplicate as my question is focused on an updated version of my code, and really I am trying to understand why this bug is occurring), I discovered that I can use a Vector3 to gain more control over where the camera ends up. It was proposed by another user that the Vector2 position moved my camera to a different Z position, and that if I move my camera to a negative Z, this should fix my problem.

My code now looks like this, and is in fact working correctly:

    public void constrain(GameObject obj, bool isCamera = false)
    {
        Vector3 pos = obj.transform.position;
        pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
        pos.y = Mathf.Clamp(pos.y, -maxY, maxY);
        if(isCamera)
            pos.z = -10;
        else
            pos.z = 1;
        obj.transform.position = pos;
    }

    GameObject camera = GameObject.Find("Main Camera");
    constrain(camera.gameObject,true); //if you are constraining the camera object

    GameObject player= GameObject.Find("Player");
    constrain(player.gameObject,true); //skip the optional parameter if not referencing the camera

It has however been proposed by yet another user that I should not need to have the optional bool parameter to check if it is the camera. The camera should not have to change its Z position, as this is set in the line reading:

Vector3 pos = obj.transform.position;

I should thus be able to simplify the function as follows:

    public void constrain(GameObject obj, bool isCamera = false)
    {
        Vector3 pos = obj.transform.position;
        pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
        pos.y = Mathf.Clamp(pos.y, -maxY, maxY);            
        obj.transform.position = pos;
    }

    GameObject camera = GameObject.Find("Main Camera");
    constrain(camera.gameObject,true); //if you are constraining the camera object

    GameObject player= GameObject.Find("Player");
    constrain(player.gameObject); //skip the optional parameter if not referencing the camera

However, when I do this the player character, and (funnily enough only sometimes) the other game objects do not display on the camera (enemies for instance are spawned at random locations and then move towards the player character, and only display if they start on screen).

I have tweaked the camera's Z position during run time, and if I move it into a negative Z position I can see all of the game objects.

Here is a screen shot of the game without moving the camera: enter image description here

And another after I moved the camera to -1 Z position: enter image description here

My question in short then is this...

When I leave my camera alone and do not constrain it, it works fine. As soon as I constrain it, it starts getting screwy. If I constrain it and move it to a negative Z position, it works.

Can someone shed some light on why this might be happening? Is the first more complicated function I posted above the sleekest solution, or does anyone have any advice on how to improve my code?

Any advise would be greatly appreciated

UPDATE It was suggested that I try placing my objects on separate sorting layers. I tried this, but it does not seem to work. Unless there is a way to move the camera to another sorting layer, I'm not sure that this is the solution...

Here is a screenshot of what I did just in case I made a mistake: enter image description here

Upvotes: 1

Views: 2061

Answers (2)

Technivorous
Technivorous

Reputation: 1712

imagine your looking at your gameobject, in fact theres two of them, A and B

A                 z=1
    B                z=0 


  V 

-this is your camera, note the angle of the v? this is your veiwport. notice if you were the camera looking at a and b they would both be in site(z=-3)

A V
    B 

-now note the camera here at the zero position, all of your objects are beside or behind it, and therefore nothing shows on screen. this is called perspective. the further back the camera, the wider its line of site, and the more objects in the view

Upvotes: 1

Morasiu
Morasiu

Reputation: 1337

TL; DR

That's how camera works in Unity.


Longer version:

When camera is on position 0 z, even in ortographic mode it's just equal to objects, so it cannot "see" object in the same plane. If you are working with 2D game (not 2,5D) you should use Sorting Layers to make nice objects hierarchy.

Upvotes: 2

Related Questions