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_