简体   繁体   中英

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. 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).

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. 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.

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

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;
}

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