简体   繁体   English

在Unity中获取许多游戏对象之间的中心点

[英]Get the center point between many GameObjects in Unity

I have created a game in which you can control X characters at the same time in the same form and they can die at any time.我创建了一个游戏,您可以在其中以相同的形式同时控制 X 个角色,并且他们可以随时死亡。 My problem is when I want the game camera to include all these gameobjects.我的问题是当我希望游戏相机包含所有这些游戏对象时。

I thought that a good option is to calculate the central point between the gameobjects in the scene and make the camera follow that point at a certain distance.我认为一个不错的选择是计算场景中游戏对象之间的中心点,并使相机在一定距离处跟随该点。

I already have the camera code, but I still need to know how to get that central point or another way of doing it.我已经有了相机代码,但我仍然需要知道如何获得中心点或其他方式。 In addition, the camera does not follow any of the axes (X, Y, Z) linearly, since it is placed in such a way that is the view is isometric (the game is in 3D).此外,相机不会线性地跟随任何轴(X、Y、Z),因为它的放置方式是视图是等距的(游戏是 3D 的)。

As a last important fact, it is that all gameobjects that are running in the game (that are alive), are stored in a public static List <GameObject> to be able to access the components of these gameobjects at any time.最后一个重要的事实是,游戏中运行的所有游戏对象(活着的)都存储在public static List <GameObject> ,以便能够随时访问这些游戏对象的组件。 Also, if a character (gameobject) dies or is born, the list is updated without problems.此外,如果角色(游戏对象)死亡或出生,则列表会毫无问题地更新。

I leave you a graphic example with three different cases, being the black points, the characters that are in the scene (gameobjects) and the red points, the central point (vector) that I would like to find.我给你留下了一个带有三种不同情况的图形示例,分别是黑点、场景中的角色(游戏对象)和红点、我想找到的中心点(矢量)。

在此处输入图片说明

Also, I leave the camera code so you can test if you have any solution:另外,我留下了相机代码,以便您可以测试是否有任何解决方案:

public class Camera_Movement : MonoBehaviour {

    Vector3 newPos;
    public static List<GameObject> playersInGame = new List<GameObject>();

    void Update() {

        // Get Central Vector

        // Replace playersInGame[0].transform.position with central vector
        //newPos = Vector3.Lerp(gameObject.transform.position, "central vector", Time.deltaTime);

        newPos = Vector3.Lerp(gameObject.transform.position, playersInGame[0].transform.position, Time.deltaTime);
        gameObject.transform.position = new Vector3(newPos.x, newPos.y, newPos.z);

    }
}

Thank you very much in advance!非常感谢您提前!

You need to take the average x and and average y.您需要取平均值 x 和平均值 y。

That would look like the following:如下所示:

var totalX = 0f;
var totalY = 0f;
foreach(var player in playersInGame)
{
     totalX += player.transform.position.x;
     totalY += player.transform.position.y;
}
var centerX = totalX / playersInGame.Count;
var centerY = totalY / playersInGame.Count;

Let me know if this works for you (don't have access to Unity at the moment), but I put together an example here: https://dotnetfiddle.net/jGd99I让我知道这是否适合您(目前无法访问 Unity),但我在这里整理了一个示例: https : //dotnetfiddle.net/jGd99I

If you want to have a solution that your camera can be best positioned to see all of your objects, then try this如果您想要一个解决方案,让您的相机可以最佳定位以查看所有对象,请尝试此操作

public Vector3 FindCenterOfTransforms(List<Transform> transforms)
{
    var bound = new Bounds(transforms[0].position, Vector3.zero;
    for(int i = 1; I < transforms.length; i++)
    {
        bound.Encapsulate(transforms[i].position);
    }
    return bound.center;
}

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

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