|
Hi, I can't figure out how to get the clones of a prefab to instantiate on the position of the empty game object that this script is on. All the objects that spawn come from the world space of 0,0,0. I want them to spawn FROM 0,0,0 of the LOCAL objects spot. (ie. When I move the parent object, the spawning of the cloned prefabs will follow.) Here is my code I'm using.
=====Old code above. Newer code below=========
(comments are locked)
|
|
clone.transform.parent = transform; would be my guess. Assumes 'this' object the script is on is the parent?
The 'position' in Instantiate will be world coordinates. Setting the 'parent' will adjust those coords to be relative to the parent, and remain same place in the world. So setting localPosition last should force them to be relative to the parent. Ok that seems to work but it has an issue... When I put it above where the clones are instantiated, it doesn't know what 'clone' is. However when I put it under where the clone is instantiated already, they all spawn on the WORLD 0,0,0 still, however when I move the parent object, they follow along the local axis of the parent object. How do I incorporate taht into my code to have them 'spawn' on the local 0,0,0
Apr 05 '11 at 12:57 AM
Musclegunner
clone.transform.localPosition = Vector3.zero;
Apr 05 '11 at 01:28 AM
DaveA
Actually, in your case, I think you'd set clone.transform.localPosition = pos;
Apr 05 '11 at 01:29 AM
DaveA
Well I'm sorry to say but nothing seems to be working. I believe the line to be the problem child is clone = Instantiate(prefab, pos, Quaternion.identity); which I now have as clone = Instantiate(prefab, pos, transform.rotation); Which is essentially the exact same thing. If I put your code before that line, Unity understandably freaks out because 'clone' hasn't been created yet. However if I put the code after this line, it has no affect at all because the 'clones' were created already. It's driving me nuts O.o It's probably a simple fix but I'm thinking too hard
Apr 05 '11 at 04:14 AM
Musclegunner
You have to put them after, they need to have been created. Setting the position after creation should work.
Apr 05 '11 at 04:23 AM
DaveA
(comments are locked)
|

First off, sorry about the formatting I'm having trouble trying to paste a code block but I'll figure out in due time
Select code, hit the 'code' button on the toolbar, looks like 1's and 0's