Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the center point between many GameObjects in Unity

Tags:

c#

unity3d

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.

enter image description here

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!

like image 592
Vicky Vicent Avatar asked Sep 17 '18 21:09

Vicky Vicent


People also ask

How do you find the middle of two objects?

Midpoint formula: M = ( (x_1 + x_2)/2, (y_1 + y_2)/2 )

How do you center in unity?

Select the object in the Hierarchy view, then put your cursor over the Scene view and press F.


2 Answers

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

like image 103
Michael Avatar answered Oct 18 '22 01:10

Michael


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.Count; i++)
    {
        bound.Encapsulate(transforms[i].position);
    }
    return bound.center;
}
like image 39
Hao Wu Avatar answered Oct 18 '22 01:10

Hao Wu