Need help for direction calculation.


I need help regarding a question related to maths/vector.
Attached scripts contain the flocking algorithm. All the flocks follow the target/goal (as shown in the below image). Everything is working fine when the placement of the manager script is at position 0,0,0 x,y,z coordinates. But I don’t them to be placed at

So, I want the help for editing the script. I want as I can place the manager at anywhere in the World and result should be same.

Following is the reference from where I got the source code/tutorial:

Source files:

Pastebin code for the same scripts:

flock.cs: flock.cs -

globalFlock.cs: globalFlock.cs -

I just edited the source files just to make all the fishes child of the parent:

P.S. I think the issue is in the flock.cs there is a line just after the if(turning)
which is calculating the direction:

Vector3 direction = - transform.position;

Please help me out.



Hey there, I took a look at the video (not all of it) and it looks like is used quite often to reference the ‘center’ of the ‘tank’. That is very unfortunate! :frowning:

I would create a new Vector3 variable (or not even, if FishManager is the center of the ‘tank’, and never moves, you can just reference its transform.position) and go through the globalFlock script changing to transform.position, where appropriate. And the Flock script will require a reference to the FileManager’s position. You can either create a variable for this in the flock script and assign it, directly after instantiate, like:

			allFish*.GetComponent<Flock>().yourVector3Variable = transform.position;*

Or, it looks like parenting the fish to the FileManager might be a good idea, and after instantiated, write:
_ allFish*.transform.parent = transform;_
Then you can reference the FishManager’s position, or the center of the ‘tank’, by using, transform.parent.transform.position, in the Flock script.
The last issue I saw is the random spawning of the fish, from the globalFlock script. The Vector3 variable “pos” is generated by negative and positive values of “tankSize”, which would only give a position around I think you can fix this one easily though, by adding “+ transform.position” when the fish is instantiated, like:
Instantiated(fishPrefab, pos + transform.position, Quaternion.identity);*_
This should add the position of the FishManager into account.
Again, I didn’t watch the entire video, so I hope this is accurate enough. :wink:
Best of luck!