Dynamic Camera and Array [SOLVED]

FIRST SCRIPT

public class CameraController : MonoBehaviour {
    public static ArrayList cameraNodes = new ArrayList();
    public Transform player;
    private bool loopHandle = true;
    public int node;

	void Start ()
    {
        Vector3 vector = new Vector3();
        CameraNode camera = new CameraNode(0, vector);
        while (loopHandle)
        {
            if (cameraNodes.Count > 0)
            {
                for (int i = 0; i < cameraNodes.Count; i++)
                {
                    camera = (CameraNode)cameraNodes*;*
 *if (camera.range >= Vector3.Distance(player.position, camera.position))*
 *{*
 *node =i;*
 *}*
 *}*
 *}*
 *camera = (CameraNode)cameraNodes[node];*
 *transform.position = camera.position;*
 *executeWait();*
 *}* 
*	}*
*	void Update ()*
 *{*
 *transform.LookAt(player);* 
*	}*
 *private void executeWait()*
 *{*
 *StartCoroutine(Wait(1));*
 *}*
 *private IEnumerator Wait(float seconds)*
 *{*
 *yield return new WaitForSeconds(seconds);*
 *}*
*}*
*public class CameraNode*
*{*
 *public Vector3 position;*
 *public float range;*
 *public CameraNode(float range, Vector3 position)*
 *{*
 *this.position = position;*
 *this.range = range;*
 *}*
*}*
*
*

What I’m trying to do with this code is dynamically change the main camera position to one of the “CameraNodes” when the character enters the range of each of them.
I have different “emtpy game objects” with the 2nd script attached and the main camera with the first script attached to it,right now its actually crashing unity,I realize I have an infinity loop on the Start() function but it has a wait so it doesnt overloads the cpu. Any insight? Thanks in advanced.
SECOND SCRIPT


public class NodeController : MonoBehaviour {
public float range;
public static int numNodes=0;
public int nodeNumber;

  • void Start () {*
    CameraNode myNode = new CameraNode(range, transform.position);
    CameraController.cameraNodes.Add(myNode);
    numNodes++;
    nodeNumber = numNodes;
  • } *
    }

The variable loopHandle is never set to false, what makes the while infinite - and it’s creating a Wait coroutine each iteration, what may produce thousands of Wait instances running at the same time, as @PaxNemesis said.

I would use a simpler and less CPU intensive approach: tag all nodes as “Node”, find and save them at Start in a member variable nodes, create also a ranges array and fill it at Start with the node ranges, and finally find the nearest node and place the camera there. In Update, check if the player has exited the range - if so, find the nearest node and move the camera to its position.

That’s the first script (attached to the camera):

public class CameraController : MonoBehaviour {
    public Transform player; // drag the player here
    public int curNode = 0;

    GameObject[] nodes; // will save the node objects here
    float[] ranges; // will save the ranges here

    void Start ()
    {
        nodes = GameObject.FindGameObjectsWithTag("Node"); // get all nodes
        ranges = new float[nodes.length]; // create the range array...
        for (int i = 0; i < nodes.length; i++){ // and fill it
            ranges _= nodes*.GetComponent< NodeController>().range;*_
 _*}*_
 _*GotoNearestNode(); // place the camera at the nearest node*_
 _*}*_
 _*void Update ()*_
 _*{*_
 _*float dist = Vector3.Distance(player.position, nodes[curNode].transform.position);*_
 _*if (dist > ranges[curNode]){ // if player out of current range...*_
 _*GotoNearestNode(); // move camera to the nearest one*_
 _*}*_
 _*transform.LookAt(player); // aim the player*_
 _*}*_
 _*private void GotoNearestNode()*_
 _*{*_
 _*float dist = Mathf.Infinity;*_
 _*Vector3 pos = player.position;*_
 _*for (int i = 0; i < nodes.length; i++){*_
 _float d = Vector3.Distance(pos, nodes*.transform.position);*_
 _*if (d < dist){*_
 _*dist = d;*_
 _*curNode = i;*_
 _*}*_
 _*}*_
 _*// move the camera to the nearest node found*_
 _*transform.position = nodes[curNode].transform.position;*_
 _*}*_
_*}*_
_*
*_

The node script would become very simple - just the range variable (remember to set the node tag to “Node”):


public class NodeController : MonoBehaviour {
public float range; // set the range for each node object
}

NOTE: This is untested C# code written by a JS guy! It may contain logic errors, but more likely will produce lots of C# cryptic errors. Let me know if you have any problem.