Get a central point among several GameObjects

0

I have created a game in which you can manage X characters at the same time in the same way and they can die at any time. My problem comes when I want the game camera to encompass 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 I get that central point or if there is another feasible way to do this. In addition, the camera does not follow any of the axes (X, Y, Z) in a linear way, since it is placed in such a way so that an isometric view is seen (the game is in 3D).

As a last important fact, it is that all the gameobjects that are running in the game (that are alive), are stored in public static List<GameObject> to be able to access the components of these gameobjects at any time. In addition, 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 achieve.

Also, I leave the code of the camera so you can do tests if you have any solution:

public class Camera_Movement : MonoBehaviour {

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

    void Update() {

        // Obtener vector central

        // Sustituir playersInGame[0].transform.position por el vector central
        //newPos = Vector3.Lerp(gameObject.transform.position, "vector central", 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!

    
asked by Vicky Vicent 18.09.2018 в 00:09
source

1 answer

0

In the end, the solution is simpler than I thought. I just needed to apply the basic mathematics that, due to tiredness, I had not even thought about them and thanks to a StackOverflow partner , I have managed to make it work.

Here the final result:

public class Camera_Movement : MonoBehaviour {

    Vector3 newPos;
    float totalX, totalY, totalZ;

    void Update() {

        // Si todos los gameobjects manejables desaparecen, no volverá a entrar.
        if (Game_Controller.playersInGame.Count != 0) {

            // Obtener vector central = Sumar todos los valores de cada eje para después
            // dividirlos por el número total de gameobjects
            // Nota: La "List<GameObject> playersInGame" esta alojada en el script "Game_Controller"
            for (int i = 0; i < Game_Controller.playersInGame.Count; i++) {
                totalX += Game_Controller.playersInGame[i].transform.position.x;
                totalY += Game_Controller.playersInGame[i].transform.position.y;
                totalZ += Game_Controller.playersInGame[i].transform.position.z;
            }
            Vector3 center = new Vector3(totalX / Game_Controller.playersInGame.Count, totalY / Game_Controller.playersInGame.Count, totalZ / Game_Controller.playersInGame.Count);

            // Se hace una interpolación entre la posición de la cámara y el centro de forma progresiva
            newPos = Vector3.Lerp(gameObject.transform.position, center, Time.deltaTime);
            // Se le da la nueva posición a la cámara
            gameObject.transform.position = new Vector3(newPos.x, newPos.y, newPos.z);

            // Se restablece los datos a 0 para volver a obtener otro centro actualizado
            totalX = 0;
            totalY = 0;
            totalZ = 0;
        }
    }
}

This camera does not zoom-in / zoom-out , if it happens that a gameobject is out of view of the camera, but it would be a good update of the above code. If someone wants to include it and get it, edit this answer and share it with the whole community.

Thank you very much everyone!

NOTE: The original post is published in StackOverflow in English. I have posted it here because I find it interesting to expand the community in Spanish with questions that have not been resolved or asked.

    
answered by 18.09.2018 / 00:09
source