A friend of mine asked me a question yesterday, it copies the Gameobject under one of the sub-nodes in the hierarchy view to the sub-nodes of another object, and he uses the method transform.parent = parent But the coordinates are all messed up after the copy. As shown in our setting up a test environment, my goal is to copy the A2 under B2.
As shown, A0 A1 and B0 B1 as parent nodes, in order to verify the feasibility of the test, I put their coordinates rotated and scaled completely messed up.
One. Without changing the copy coordinates
1. Direct copy this is the simplest
1234 |
private void movegameobject (gameobject copy, Transform parent) { copy. Transform. Parent = parent } |
2. Instantiate a new copy, and then in copy, focus on this, because the problem is also here.
12345678910 |
private void copygameobject(gameobject copy,Transform Parent ) { gameobject go = Instantiate(copy) as gameobject; go. Transform. Parent = parent; go. Transform. Position = copytransform. Position go. Transform. Rotation = copy. Transform. Rotation; Vector3 pLoss = copy. Transform. Lossyscale; Vector3 panelloss = parent. Lossyscale; Go.Transform.Localscale = New Vector3((ploss. X/panelloss. X , (pLossy/panelloss.y) , ( ploss. Z/panelloss. Z } |
a. Gameobject go = Instantiate (copy) as Gameobject;
Instantiate a new gameobject in the root directory, at which point the coordinates, rotation, and scaling coefficients take the relative coordinates of copy. Then the coordinates of this step are already chaotic.
b. go.transform.parent = parent;
Only the action of the move, converted to the relative parent's coordinate rotation scaling.
c. go.transform.position = copy.transform.position;
Go.transform.rotation = copy.transform.rotation;
The position, rotation, and scale seen in Unity's transform components are relative to the parent node, and are not relative to the world. So it's time to copy the world coordinate system of the "copied" to the "copy-person"
d. Vector3 PLoss = Copy.transform.lossyScale;
Vector3 Panelloss = Parent.lossyscale;
Go.transform.localScale = new Vector3 ((ploss.x/panelloss.x), (PLOSS.Y/PANELLOSS.Y),
Because unity cannot set the world scale factor directly, only the relative scaling factor can be set. So here I'm going to use Lossyscale to change it. Relative coordinates, Lossyscale is a read-only property, which is a world scale factor that reads only an object.
Two. In case of changing the copy coordinates
This situation is typically instantiated from Resources.load. After the instantiation is completed, the pre-set copy of the prefab is given to the object after the Instiate ().
123456789 |
private void loadprefab(string name,Transform parent) {gameobject prefab Span class= "Crayon-o" >= resources.load<gameobject>< Span class= "Crayon-sy" > (name gameobject go = Instantiate(prefab) as gameobject; go. Transform. Parent = parent; go. Transform. Localposition = Prefab. Transform. Position go. Transform. Localrotation = Prefab. Transform. Rotation go. Transform. Localscale = prefab. Transform. Localscale; } |
In fact, most of the cases are localpostion:000 localrotation:000 localscale:111.
123456789 |
private void loadprefab(string name,Transform parent) {gameobject prefab Span class= "Crayon-o" >= resources.load<gameobject>< Span class= "Crayon-sy" > (name gameobject go = Instantiate(prefab) as gameobject; go. Transform. Parent = parent; go. Transform. Localposition = Vector3. Zero go. Transform. Localrotation = Quaternion. Euler (vector3. Zero go. Transform. Localscale = Vector3. One} |
Relative coordinates, rotation localposition localrotation (can be seen in the transform component)
World coordinates, rotation position rotation (cannot be seen in the editor, can be output by code)
Only Localscale is more special, its world scale factor Lossyscale is read-only, but cannot be written.
Copy the last two completely coincident:
- This article fixed link: http://www.xuanyusong.com/archives/3151
- Reprint Please specify: Rain pine Momo August 22, 2014 Yu Yussong Momo Program Research Institute published
"Reprint" Unity3d Institute Transform.parent = Parent coordinates are messed up.