1
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCameraBehind : MonoBehaviour
{
    public GameObject camera;
    public List<GameObject> targets = new List<GameObject>();
    public float cameraDistance = 10.0f;
    public bool behindMultipleTargets = false;
    public string cameraWarningMsgs = "";
    public string targetsWarningMsgs = "";

    // Use this for initialization
    void Start()
    {
        if (camera == null)
        {
            var cam = GetComponent<Camera>();
            if (cam != null)
            {
                cameraWarningMsgs = "Gettig camera component.";

                camera = transform.gameObject;
            }
            else
            {
                cameraWarningMsgs = "Creating a new camera component.";

                GameObject NewCam = Instantiate(new GameObject(), transform);
                NewCam.name = "New Camera";
                NewCam.AddComponent<Camera>();
                camera = NewCam;
            }
        }

        if(targets.Count == 0)
        {
            targetsWarningMsgs = "No targets found.";
        }
    }

    void FixedUpdate()
    {
        if (targets.Count > 0)
        {
            MoveCameraToPosition();
        }
    }

    public void MoveCameraToPosition()
    {
        if (targets.Count > 1 && behindMultipleTargets == true)
        {
            var center = CalculateCenter();
            transform.position = new Vector3(center.x, center.y + 2, center.z + cameraDistance);
        }

        if (behindMultipleTargets == false)
        {
            Vector3 center = targets[0].transform.position - targets[0].transform.forward * cameraDistance;
            transform.position = new Vector3(center.x, center.y + 2, center.z);
        }
    }

    private Vector3 CalculateCenter()
    {
        Vector3 center = new Vector3();

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

        center = new Vector3(centerX, centerY);

        return center;
    }
}

The CalculateCenter function make the targets(objects) to change positions and vanish away far away. Even if there is only one single target.

What I want to do is if there is one object for example one 3d cube position the camera behind the cube. And if there are more cubes for example two or ten and the camera is somewhere else calculate the middle position behind the targets and position the camera in the middle behind them.

To show what I mean in this example the view(like a camera) is behind the two soldiers in the middle position between them from behind.

But what if there are 5 soldiers how can I find the middle position and then position the camera behind them like this example in the screenshot ?

View example

\$\endgroup\$
3
  • \$\begingroup\$ I do not see how your CalculateCenter() function can make the targets move. Are the targets parented to the object this script is on? Please show your object hierarchy in Unity. \$\endgroup\$
    – Sirius 5
    Mar 27, 2020 at 15:06
  • 1
    \$\begingroup\$ Are you sure you only need the x & y coordinates? What about the z-coordinate? Remember that y is "up" in Unity. Have you tried calculating the math by hand to see what you get? \$\endgroup\$
    – Sirius 5
    Mar 27, 2020 at 15:10
  • \$\begingroup\$ @Sirius5 I didn't try by hand. Should I try first drawing it on a paper and try to figure it on my own ? \$\endgroup\$
    – Daniel Lip
    Mar 27, 2020 at 19:58

1 Answer 1

1
\$\begingroup\$

I wanted to comment but I can't yet (not enough reputation) so I'll ask here:

  1. The CalculateCenter function make the targets(objects) to change positions

I'm not sure it's true, since this function doesn't change any transform of any object. I assume you meant the calculation in this function is incorrect? See 2 for that.

  1. You said the objects vanish far away - since you're talking about 3D object, maybe it's because you haven't set a z value in your CalculateCenter function? Try calculating a centerZ and adding it to the Vector3 constructor.

BTW the

new Vector3()

is redundant since you're reassigning a new Vector3 to center later on that function :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .