How to add instantiated objects to position constraint source list during runtime?


I want to have my camera follow an empty object, and I want this empty object to be located roughly in the center of a scene of moving objects, using a position constraint to stay in the average center of those objects.

These moving objects will be instantiated and destroyed randomly over time, but I still want the empty object to remain in the general center of these active objects.

I’m playing with Position Constraint scripts and trying to create an array of the moving objects that will be instantiated in the scene, but I don’t know how to use the moving objects array as a source for the Position Constraint component.

Any advice would be much appreciated!

Hi, this is a script I made for you that if I understand what you are trying to do should work perfectly, be aware though that it looks crazy in the inspector in playmode but it works:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Animations;
using UnityEngine;

public class ClassName : MonoBehaviour
    public PositionConstraint positionConstraint;
    public string ConstraintTag = "ObjectToBeFollowedByCamera";// this is the tag to connect to
    // Start is called before the first frame update
    void Start()
        positionConstraint = GetComponent<PositionConstraint>();

    // Update is called once per frame
    void LateUpdate()// late update so it happens between screen refreshes
        List<ConstraintSource> sources = new List<ConstraintSource>();
        // disable th position constraint so that it does not make the 
        // object jump around while removing and adding targets
        positionConstraint.enabled = false;
        // this for loop deletes all the sources
        // in the position constraint component
        for (int i = 0; i < sources.Count; i ++)
        // this foreach loop adds all the gameobjects
        // with tag defined by the ConstraintTag variable
        // to the list of sources on the position constraint
        foreach (GameObject x in GameObject.FindGameObjectsWithTag (ConstraintTag) )
            // create new source variable
            ConstraintSource source = new ConstraintSource();
            // initializes the variable for x's transform
            source.sourceTransform = x.GetComponent<Transform>();
            // initializes the variable for x's weight just like the 
            // weight option in the position constraint component
            source.weight = 1;
            // add the source to the position constraint
        // enable the position constraint to place the object in the correct position
        positionConstraint.enabled = true;

in this script the Array you speak of is the Array provided by the findGameObjectsWithTag() part and then sources list is all the targets the position constraint is using.
don’t forget to make sure your class name and file name match, and that all your objects are tagged with whatever you choose the tag to be. and place this script on your empty game object and you should be good to go!