我在Unity中创建了一个2D块堆叠游戏,你可以实例化x轴平移派生器对象中的块。我面临的问题是,当堆栈变得太高并接近产卵器时,我需要在Y轴上移动产卵器和游戏摄像头。我正在测量一堆预制板,以便使用边界来获得它们的高度,这样我就知道什么时候移动相机和产卵器。我面临的问题是,当我调用GetMaxBounds函数并封装它没有添加到parentBounds变量中的附加点时。我的parentbums.max.y从不增加。我是编程和C#的新手。提前谢谢。
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;
}
发布于 2017-07-07 14:12:34
保留一个计数器,它将存储堆栈中有多少对象出现在屏幕上,并在每次将项目添加到堆栈中时递增它。然后,考虑到您保存了一个项目的大小,您只需将您的大小乘以堆栈中存在的项目的数量。只需将此大小与场景的当前垂直大小进行比较,并在堆叠达到屏幕垂直大小的80%时移动相机。
当你这样做的时候,记住要重置项目的数量,因为你可能会向上移动你的相机,并且大多数在堆栈较低级别的项目将被隐藏。例如,如果在移动相机时只允许3个立方体可见,只需将堆栈中的项数设置为3即可。
由于您的问题似乎包括不会以相同方式堆叠的对象,另一种解决方案是使用Bounds。假设您保留了堆栈中所有项的列表,您可以评估将添加到代表堆栈的父对象中的每个对象的边界,并通过封装每个子边界来评估垂直大小。您可以在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;
}
然后,您可以通过将计算的Bounds
对象的max
y值减去min
y值来计算垂直大小。所以你可能会有类似这样的东西:
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;
}
同样,如果您要将相机向上移动,请确保仅在屏幕上实际显示的项目上使用此解决方案。
https://stackoverflow.com/questions/44972704
复制相似问题