Problems In Flocking Algorithm

function Start () {
}

function Update () {
raptorarray= GameObject.FindGameObjectsWithTag(raptor.tag);

if ( raptorarray.length == 1)
     return;
else 
 {
 			for (var l = 0;l<raptorarray.length;l++)
 			transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);		
 }
}



function rule1(rap : GameObject) //keep to the average position of the flock
{
	var pos : Vector3 = Vector3.zero;
	for (var i= 0; i<raptorarray.length;i++)
	{
		pos+=raptorarray*.transform.position;*
  • }*
  • averagepos=pos/raptorarray.Length;*
  • return averagepos/100;*
    }

function rule2(rap : GameObject) //avoid other objects including others in the flock
{

  • var away : Vector3 =Vector3.zero;*
  • for (var i= 0; i<raptorarray.length;i++)*
  • {*
    _ var distance :float = Vector3.Distance(rap.transform.position, raptorarray*.transform.position);_
    _
    if (distance < 1)_
    _ keepaway = rap.transform.position + (rap.transform.position - raptorarray.transform.position);
    }
    return keepaway;
    }*_

function rule3(rap : GameObject) //match the velocity of the others in the flock
{
* for (var k=0; k<(raptorarray.length);k++)*
* { *
* for (var i= 0; i<(raptorarray.length-1);i++)*
* {*
_ if(rap.rigidbody.velocity != raptorarray*.rigidbody.velocity)
{
rap.rigidbody.velocity+= raptorarray.rigidbody.velocity;
rap.rigidbody.velocity/=2;
}
}
}
matchvel=rap.rigidbody.velocity/8;
return matchvel;
}*

The problems are that whenever the 2nd enemy prefab is instantiated at any time, both the first and the second disappear and I get an error message "transform.position assign attempt for ‘Enemy 1(Clone)’ is not valid. Input position is { 244287365142033000000000000000000000000.000000, 11839572652397211000000000000000000000.000000, Infinity }.
UnityEngine.Transform:set_position(Vector3)
Flocking:Update() (at Assets/Flocking.js:20)"
Normally the Enemy AI would randomly roam looking for the character if not closeby and move towards the user if closeby
The same message shows in both cases
the bug is allegedly in the line “transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);” but since there are 3 procedure calls in that line of code I’m not going to discount the possibility of a bug being in one of the procedures.
I’m also wondering if rule 3 is redundant since all enemies are supposed to be “spawned” with the same velocity_

I’ve got my doubts about the logic in a couple of sections of your code, but the thing that’s actually causing the error is, as you say, here:

for (var l = 0;l<raptorarray.length;l++)
transform.position = transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);

You’re looping through each element in the raptorarray and then adding the three rules of every element to the transform.position of the gameobject to which this script is attached. This is causing the huge numbers you quote, which would result in an arithmetic overflow.

If this script is attached to a “manager”-type object that’s meant to set all the raptor positions on their behalf, then I’m pretty sure what you meant was to loop through each element in the raptorarray and set their own transform.position based on the three rules. Something like:

for (var l = 0;l<raptorarray.length;l++)
raptorarray[l].transform.position = raptorarray[l].transform.position + rule1(raptorarray[l]) + rule2(raptorarray[l]) + rule3(raptorarray[l]);

Alternatively, if this script is attached to each object in the raptorarray directly, then you need to get rid of

for (var l = 0;l<raptorarray.length;l++)