2
\$\begingroup\$

Apologies if this is a basic question, or been asked a 1,000 times in the past...

I'm new to Unity, and having problems understanding some of the basics to do with 2D cameras (orthographic), and while I've read a lot nothing has made me have that 'ah-ah!' moment yet.

What I'm trying to create is the following (camera position depicted by the box with the pink dashed line) - where the yellow filled boxes are made up of a whole bunch of sprites.

Overtime, the camera need to move left to right, and then bottom to top...

Camera movement

What my issue seems to be is understanding how I can get the camera to display all of the sprites - in other words, resize the camera view to fit the background sprite's height, and know when to stop the camera in order to start moving it up.

What I'd like to understand is how and why to solve this problem - it's the only way I'll learn!

Thanks

Kieron

\$\endgroup\$
10
  • \$\begingroup\$ Is the idea that you are trying to make the camera automatically move to follow the map (the background images) while the player moves through the level? \$\endgroup\$
    – Alan Wolfe
    Jun 15, 2015 at 4:18
  • \$\begingroup\$ This is part of an introduction, so there is no player at the moment. It's purely moving the camera according to the background sprites position and bounds...which may change as the game develops. \$\endgroup\$
    – Kieron
    Jun 15, 2015 at 4:22
  • \$\begingroup\$ I agree that making the camera do this automatically could be nice, but another route if you are interested in alternatives is to script the camera by hand to tell it what points to be at, and at what zoom levels, at specific times. I don't know what your final game is going to be like, but there might be times when you want to "break the rules" that an automated system might have. \$\endgroup\$
    – Alan Wolfe
    Jun 15, 2015 at 4:25
  • \$\begingroup\$ Sure, in this instance it's part of an animation, designed as an introduction. I'm assuming that this is going to be entirely script based, simply because the position and bounds of the background sprite could change. \$\endgroup\$
    – Kieron
    Jun 15, 2015 at 4:29
  • \$\begingroup\$ oh ok, so your question is about how if you have an image that is (X,Y) in dimensions, and the center is at point P, how do you set up the camera such that it shows the sprite? Basically you are just trying to figure out how to make your orthographic camera show a specific rectangle of world coordinates? \$\endgroup\$
    – Alan Wolfe
    Jun 15, 2015 at 4:32

1 Answer 1

5
\$\begingroup\$

I wrote a camera script that auto-zooms based on a transform's position and scale.

Attach the script below to your Camera. Then, create a GameObject, and set its position and size. Then link this gameobject to the "Area" public property of this camera script.

The "Area" GameObject can be adjusted at runtime, and should provide you with a mechanism to at least get you started on what you are attempting to do.

I put the logic inside of Update() to simplify, however, you will most likely want to move this in a function that you would call manually when a recalc needs to be done.

using UnityEngine;
using System.Collections;

public class CameraZoom : MonoBehaviour
{
    public Transform Area;

    public void Update()
    {
        float height = Area.localScale.y * 100;
        float width = Area.localScale.x * 100;

        float w = Screen.width / width;
        float h = Screen.height / height;

        float ratio = w / h;

        float size = (height / 2) / 100f;

        if (w < h)
            size /= ratio;

        Camera.main.orthographicSize = size;

        Vector2 position = Area.transform.position;

        Vector3 camPosition = position;
        Vector3 point = Camera.main.WorldToViewportPoint(camPosition);
        Vector3 delta = camPosition - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
        Vector3 destination = transform.position + delta;

        transform.position = destination;
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ That looks great, thank-you! I'll try it out, hopefully over the weekend. Cheers! \$\endgroup\$
    – Kieron
    Jun 19, 2015 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .