- Home /
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[i].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[i].transform.position);
if (distance < 1)
keepaway = rap.transform.position + (rap.transform.position - raptorarray[i].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[i].rigidbody.velocity)
{
rap.rigidbody.velocity+= raptorarray[i].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.
I just fixed that issue - but that wasn't the bug I was looking for =/
Just 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]);
(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
You 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.
Answer by tanoshimi · Dec 08, 2013 at 08:30 PM
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++)
The script is attached to a prefab, the prefab is spawned itself rather than something else managing all the enemy prefabs for me - I'm guessing that's what you meant by attached to each object. I arranged that but it's still not working - should I just start the algorithm over?
Your answer
Follow this Question
Related Questions
Networking Synchronize Problem. 0 Answers
Enemy AI With changing Player 0 Answers
AI walk through solid walls 1 Answer
why is collider not colliding? 1 Answer
How to make an enemy backpedal? 1 Answer