- Home /
Here, no one is pretty much available for the detailed task.
Need help for direction calculation.
Hi
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 vector3.zero.
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: https://www.youtube.com/watch?v=eMpI1eCsIyM
Source files: http://files.holistic3d.com/yt/eMpI1eCsIyM.zip
Pastebin code for the same scripts:
flock.cs: http://pastebin.com/2nAWvdK8
globalFlock.cs: http://pastebin.com/LMUzfEdM
I just edited the source files just to make all the fishes child of the parent: https://www.dropbox.com/s/9xo3olv0x8dxfj4/FishesUpdated.unitypackage?dl=0
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 = Vector3.zero - transform.position;
Please help me out.
Thanks,
Harshdeep
Answer by Lylek · Mar 10, 2017 at 11:03 AM
Hey there, I took a look at the video (not all of it) and it looks like Vector3.zero is used quite often to reference the 'center' of the 'tank'. That is very unfortunate! :(
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 Vector3.zero 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[i].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[i].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 Vector3.zero. 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. ;) Best of luck!
Hi @Lylek23 :
Thanks a lot for the reply. I agree on your all above stated points, but I have already done all the steps, but hard luck :(
However, regarding your second point. I have made the fishes child of the parent so they instantiated under them.. but I will again review that part. Thanks for second issue.
Follow this Question
Related Questions
Setting up point sets for a procedural tree 1 Answer
Get rotation from a vector direction. 1 Answer
a variable that allows you to choose either x,y, or z? 3 Answers
Plane geometry 0 Answers
Fire Knockback 3 Answers