简体   繁体   English

Unity3D-如何测量一堆物体的高度?

[英]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. 我正在Unity中创建一个2D块堆叠游戏,您将从x轴平移生成器对象实例化块。 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. 我面临的问题是,当堆栈太高且离生成器太近时,我需要将生成器和游戏摄像头沿Y轴向上移动。我正在测量一堆预制件,以使用Bounds来获取其高度,因此我知道何时移动相机和生成器。 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. 我面临的问题是当我调用GetMaxBounds函数并将其未添加到我的parentBounds变量的其他点封装时。 My parentBounds.max.y never increases. 我的parentBounds.max.y永远不会增加。 I'm new to programming and C#. 我是编程和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. 只需将此尺寸与场景的当前垂直尺寸进行比较,然后在堆栈达到屏幕垂直尺寸的80%时移动摄像机即可。

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. 例如,如果在移动相机时只显示3个多维数据集,则只需将堆栈中的项目数重新设置为3。

As your problem seems to include objects that won't stack the same way, another solution is to use Bounds . 由于您的问题似乎包含不会以相同方式堆叠的对象,因此另一种解决方案是使用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. 然后你可以通过减去的评估垂直尺寸max的评估的Y值Bounds对象的min y值。 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. 同样,如果要向上移动相机,请确保仅在屏幕上实际显示的项目上使用此解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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