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_
In 'rule2()', if no objects meet the '(distance < 1)' calculation, then 'keepaway' is never set by this routine. Not knowing how this variable is initialized, I don't know if it is an issue or not.
– robertbuI just fixed that issue - but that wasn't the bug I was looking for =/
– EbamberJust before you set the position, add a Debug.Log to isolate the issue: Debug.Log(transform.position + ", " + rule1(raptorarray[l]) + ", " + rule2(raptorarray[l]) + ", " + rule3(raptorarray[l]);
– robertbu(1167.6, 32.0, 2206.7), (11.7, 0.6, 22.4), (1167.6, 32.0, 2206.7), (0.0, -0.2, -0.4) UnityEngine.Debug:Log(Object) Flocking:Update() (at Assets/Flocking.js:20) that was the first error message I got
– EbamberYou are looking for the error message just before you get the error. According to what you outlined, one of the three functions is returning something ugly. This Debug.Log() should tell you which one.
– robertbu