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!