Stop zooming camera if I can see ALL the gameobject in view

I have a GameObject lets say it’s 1920x1080 pixels it could be smaller or larger.

currently have a zoom effect where I adjust the orthographicSize, just using the keyboard / will convert this to touch later, but what I want to do is stop the zooming effect if I can see all the gameobject in the camera view, I’m trying a few idea’s but no joy so far.

Has anybody got any idea’s on this?

Thanks

  • is the camera centered on the gameobject
  • is the gameobject of the same aspect ratio as the screen

If the answer is yes to both, it is the simplest case and the max zoom is just a factor between screen size and gameobject size.

If not, you need a little math. I would calculate the bouning box so that it is centered, then scale considering the difference in aspect ratio (i.e. No side goes out of screen space)

Edit: I have not done it, it is just and idea

@sngdan

Both the GameObject and the Camera are at Vector3.Zero

Not sure what you mean by this, the size of the gameobject could be 2560x2560 or 1920x1080 they have no set size, I would like to workout a scaling factor “orthographicSize” when it runs.

Now I did come put with a brute force approach, works on my PC, not sure about tablets/phone yet, my original moving the gameobject does work so I cannot see why this would not, the only problem with this is that IF, the screen is larger than the image when it starts it will cock up and not work, hence I’m using large images, users have to link dots/nodes around the image, so on small screen it would be impossible with out zoom.

private void CheckZoom(float oldzoom)
{
  Single mWidth = mRenderer.bounds.size.x + 4;
  Single mHeight = mRenderer.bounds.size.y + 4;
  Single mLeft = Drawing.transform.position.x - (mWidth * 0.5f);
  Single mTop = Drawing.transform.position.y - (mHeight * 0.5f);
  Rect mArea = new Rect(mLeft, mTop + 2, mWidth, mHeight);

  Vector3 lowerLeft = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
  Vector3 upperRight = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));

   mZoomLock = ZoomLock.None;

  if (lowerLeft.y < mArea.yMin)
    {
     mZoomLock = ZoomLock.Vertical;
     mMainCamera.orthographicSize = oldzoom;
    }

  if (lowerLeft.x < mArea.xMin)
    {
     if (mZoomLock == ZoomLock.None)
      mZoomLock = ZoomLock.Horizontal;
     else
      mZoomLock = ZoomLock.Both;

     mMainCamera.orthographicSize = oldzoom;
    }
}

if you have anymore idea cheers

I might not get exactly what you are trying to achieve.

Since the camera is centered on the game object, I imagine something like this:

  • you work on something resolution independent (say for example 2048x1536 or 1920x1080, I.e. 4:3 or 16:9)
  • your game objects are of arbitrary size (I.e. 960x540), just to take a simple case)

since the GO aspect is 16:9, on the 1920x1080 the max zoom (orthographic size) would be 540 / ppu / 2

On 2048x1536 it would be 960 / 4 * 3 / ppu / 2 (orthographic size) ???

Edit: yes I understood you want to calculate at runtime, that’s why I think the aspect ratio of the game object is important - but it does not need to be fixed. It just requires you to scale on the right axis, i.e. Back convert to unitiy’s 1/2 height convention

@sngdan

I think you might of miss understood, don’t forget this will run on tablets/phones not a PC, just to clear that up, I still need to zoom the camera in or out, BUT I don’t want to zoom out further away then the GameObject size can be seen in the camera whether that be width or height(if it’s locked on the height it will only be scrollable on the width), that’s what the method above does, but it brute force, sort of have to make the image larger than any screen say minimum of 3000 pixels so the zoom will work from the start, these are only two colour images.

A little code might help what you are getting at if I miss understood your reply sorry.

Yes, I am still confused. I thought the zoom is locked so that always the full object is in view (i.e. scroll where?).

My understanding is the following, which might not be want you want…but check for yourself.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
    1. Attach this to the Camera
    2. Drag game object into goZoomLock public variable
    3. Set a screen resolution in the Game view (or use free aspect)
    4. Run game
    5. Slide orthographic camera size to see the zoom lock
  
    Obviously this whole script should not run in Update if implemented
  
*/


public class CameraZoom : MonoBehaviour
{
    public GameObject goZoomLock;                    // Object to be tracked by Camera
  
    private float screenAspect;
    private float zoomAspect;
    private float maxZoom;
  
    void Update ()
    {
        goZoomLock.transform.position = new Vector2 (transform.position.x, transform.position.y);                                                    // lock object to center of camera
        zoomAspect = (float) goZoomLock.GetComponent<Renderer>().bounds.size.x / goZoomLock.GetComponent<Renderer>().bounds.size.y;                    // calculate object aspect
        screenAspect = (float) Screen.width / Screen.height;  // Camera.main.aspect should work as well                                                // calculate screen aspect

        if (zoomAspect > screenAspect)
        {
            maxZoom = goZoomLock.GetComponent<Renderer>().bounds.size.x / screenAspect / 2f;                                                        // maxZoom based on width
        }
        else
        {
            maxZoom = goZoomLock.GetComponent<Renderer>().bounds.size.y / 2f;                                                                        // maxZoom based on height
        }

        if (Camera.main.orthographicSize < maxZoom) Camera.main.orthographicSize = maxZoom;                                                            // lock Camera
    }
}

First I want to say @sngdan thanks for helping on this

Lets say that user is using a device (my Samsung 10.1 1280x800 pixels), now the image I might be using is 1969x1926 so this is larger than the screen, so I must allow zooming, BUT if the user Zooms they MUST only zoom out if the image e.g “GameObject” is larger “Out of view” than the screen so if for example I zoom out but now I can see all the image horizontally, I should only be able to scroll “move the image with my finger” in the vertical way.

I’ve uploaded my editor image “Editor-Image” and the Second Image “Editor-Image2” when I’ve zoomed out, this is to do with the information I have just explained.

public void AreaUpdate()
{
  mRenderer = Drawing.GetComponent<SpriteRenderer>();

  Single mWidth = mRenderer.bounds.size.x + 4;
  Single mHeight = mRenderer.bounds.size.y + 4;
  Single mLeft = Drawing.transform.position.x - (mWidth * 0.5f);
  Single mTop = Drawing.transform.position.y - (mHeight * 0.5f);
  mSelectorArea = new Rect(mLeft, mTop + 2, mWidth, mHeight);
}

This Calculates the Area “World Space” of the Drawing you see “This does not change”.

private void UpdateSketchPosition()
{
  switch (mZoomLock)
  {
   case ZoomLock.None:
    this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, mSelectorArea.xMin + mCameraHalfWidth, mSelectorArea.xMax - mCameraHalfWidth), Mathf.Clamp(this.transform.position.y, mSelectorArea.yMin + mCameraHalfHeight, mSelectorArea.yMax - mCameraHalfHeight), this.transform.position.z);
    break;

   case ZoomLock.Vertical:
    this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, mSelectorArea.xMin + mCameraHalfWidth, mSelectorArea.xMax - mCameraHalfWidth), 0F, this.transform.position.z);
    break;

   case ZoomLock.Horizontal:
    this.transform.position = new Vector3(0, Mathf.Clamp(this.transform.position.y, mSelectorArea.yMin + mCameraHalfHeight, mSelectorArea.yMax - mCameraHalfHeight), this.transform.position.z);
    break;

   case ZoomLock.Both:
    this.transform.position = new Vector3(0F, 0F, this.transform.position.z);
    break;
  }
}

Now the above code positions including zoom of the object of the screen, so if no Zoom lock is need we can freely move the object with our finger or mouse(“this is for testing etc”).

I hope this makes more sense and is a better guide?

Cheers.


Hi there, I still do not get it, haha.

What exactly is the problem you want to solve, can you describe the current behavior and targeted behavior ???

I thought it was about the zooming, which should be addressed in my code example above. I now understand that you do not want to restrict scrolling to the object being fully visible (its longer side) but to its shorter side, I guess you can simply change

  • if (zoomAspect > screenAspect) to
  • if (zoomAspect < screenAspect)

Since you allow scrolling (which I misunderstood) you have to comment out this line, too and use your scroll code (assuming it is working + you call it “ZoomLock”)