- Home /
How to be sure that the parent's position is the same is the child's position?
Hi All,
I have a question here. I have a list of boats which is the parent and 25 flockers who are the child of each boat. However, it seems like right now, when the boat is sailing, the flockers follow the direction of the boat but not the position of the boat. I would like to have the flockers on each of the boats and follow the coordinates of the boats as they sail as if like they are on the cruise.
This is what I have done:
for (int j=0; j<4; j++){
for (int i=0; i<25; i++){
int numberOfRows = (int)(Mathf.Sqrt(25));
int a = i/numberOfRows - numberOfRows/2;
int b = i%numberOfRows - numberOfRows/2;
Transform trans = FleetManager.getBoatTransform(j);
Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f);
pos = new Vector3(a, 0.0f, b);
GameObject go = (GameObject)Instantiate(flockerPrefabs[0], pos, Quaternion.identity);
go.transform.parent = trans;
go.transform.position = pos;
this.transform.parent = null;
}
}
public Transform getBoatTransform(int id){
BoatAnim[] array = boats.ToArray();
return array[id].transform;
}
There were a lot of suggestions that I have looked up:
(a)"•When parenting Transforms, set the parent's location to <0,0,0> before adding the child. This will save you many headaches later. " from http://docs.unity3d.com/Documentation/Components/class-Transform.html
(b)"Move the parent (via the inspector) so that the children are in the right place the if the pivot was 0, 0, 0. Then, unparent the children, set the parent to 0,0,0, and reparent" from http://answers.unity3d.com/questions/18637/change-pivot-of-parent-game-object.html
Any suggestions to make the flockers on the boat and sail according to the boat's position?
Thanks.
Kim
Hi,
Btw, at the inspector, I have also set the boats and flocker$$anonymous$$anager's position to be 0,0,0. What else that I could have missed or mistakenly done?
Thanks.
What scripts are attached to the 'flockerPrefabs[0]' prefab? If you replace this prefab with a simple game object like a sphere, do you still have the problem?
Hi,
It is a Flocking script. I have flocker$$anonymous$$anager which controls a group of flockers and flocking script which controls each of the flocker.....
Thanks.
The parent/child relationship is fairly straight forward. I cannot see how you would get direction but not position unless there is an issue with either the flocker$$anonymous$$anager or with the flocking script.
In my boat prefab, there are "shape" and "plane" added to it as children. I added the plane as a mesh collider so that the flocker will not fall into the sea. The shape basically comes by default when I downloaded from free$$anonymous$$odel.
$$anonymous$$ust the boat prefab and shape be in the same position and rotation? As in all 0,0,0?
Because when the boat sails, the flockers obviously follow but as though like with an offset point far behind the boat and not exactly with the boat. Would it be all the related Gismos are not aligned?
Any further hints? Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102839im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
How to set pivot point to the parent 1 Answer
tank turret rotate without messing up 2 Answers
change pivot of parent game object 1 Answer