Search Unity

Camera script to implement "Frame selected" in-gam

Discussion in 'Scripting' started by Unitard, Sep 19, 2008.

  1. Unitard

    Unitard

    Joined:
    Jan 27, 2008
    Posts:
    29
    I want to be able to select objects in my game and the camera automatically zooms to fit- you know, like if you hit the "F" key in the editor.

    more exactly- hitting the zoom in key will- if the selected objects are smaller than the screen, zoom in (respecting a max zoom in limit) to frame the selected objects, and keep it locked there- hitting zoom out will free it up. If they are outside the screen, it will zoom out (respecting a max zoom out limit) to frame them and stay there until the zoom in key is pressed. That's just details, though.

    The main thing is to be able to figure out the 2d bounding box of all the selected objects in screen coords. Right?

    I can't find an easy way to convert an object's world-space bounding box into a screen-space bounding rectangle in the docs. It looks like if I just use the pivot point coordinates it will be easier but this may fail for large or tall objects.
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I think that's about right, take the selected object, find its bounding box and then convert that to screen coordinates. As to the docs, this is a good place to start as WorldToScreenPoint lets you take a world position and convert it to an on-screen point:

    Camera.WorldToScreenPoint

    It seems that you ought to be able to apply that to each of the selected object's bounding box corners and do your zoom in/out determination from there.