简体   繁体   中英

Unity3D - How to measure the height of a stack of objects?

I'm creating a 2D block stacking game in Unity where you instantiate blocks from a x-axis translating spawner object. The problem I'm facing is when the stack gets too high and close to the spawner, I need to move the spawner and game camera up on the Y-axis.I'm measuring a stack of prefabs to get their height using Bounds so I know when to move my camera and spawner. The problem I'm facing is when I call my GetMaxBounds function and Encapsulate the additional points it is not adding to my parentBounds variable. My parentBounds.max.y never increases. I'm new to programming and C#. Thanks in advance.

游戏画面

if  (Input.GetMouseButtonDown(0)) {
        Instantiate (fallingBrick, spawnPosObj.transform.position, Quaternion.identity, parentStack.transform);
        var parentBounds = GetMaxBounds (parentStack);
        Debug.Log (parentBounds.max.y);
    }

}

Bounds GetMaxBounds(GameObject g) {
    var b = new Bounds(g.transform.position, Vector3.zero);
    foreach (Renderer r in g.GetComponentsInChildren<Renderer>()) {
        b.Encapsulate(r.bounds);
    }
    return b;
}

Keep a counter which will store how many objects in the stack are present on the screen, and increment it each time an item is added in your stack. Then, considering you saved the size of one item, you just have to multiply your size by the number of items present on the stack. Just compare this size with the current vertical size of your scene, and move your camera when your stack reaches for instance 80% of the vertical size of your screen.

When you do so, remember to reset the number of items, as you will probably move your camera up and most of the items that were in the lower level of your stack will be hidden. For instance, if you let only 3 cubes visible when moving your camera, just set back your number of items in the stack to 3.

As your problem seems to include objects that won't stack the same way, another solution is to use Bounds . Imagining you are keeping a list of all the items in your stack, you can evaluate the bounds of each one of the objects that would be added in a parent object representing the stack, and evaluate the vertical size by encapsulating each child bounds. You can find an example in this answer :

Bounds GetMaxBounds(GameObject stack) {
   var b = new Bounds(stack.transform.position, Vector3.zero);
   foreach (Renderer r in g.GetComponentsInChildren<Renderer>()) {
     b.Encapsulate(r.bounds);
   }
   return b;
}

And you can then evaluate the vertical size by substracting the max y value of the evaluated Bounds object to the min y value. So you could have something like:

float GetVerticalSize(GameObject stack) {
   var b = new Bounds(stack.transform.position, Vector3.zero);
   foreach (Renderer r in g.GetComponentsInChildren<Renderer>()) {
     b.Encapsulate(r.bounds);
   }

   float size = b.max.y - b.min.y;
   return size;
}

Again, if you are moving your camera up, make sure to only use this solution on the items which are actually shown on screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM