- Home /
Instantiate cloned prefab to local position of an empty object
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.
var prefab : Rigidbody;
var speed = 5;
var numberOfObjects = 20;
var radius = 5;
function LaunchingProjectile()
{
for (i = 0; i < numberOfObjects; i++)
{
var angle = i * Mathf.PI * 2 / numberOfObjects;
var pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
clone = Instantiate(prefab, pos, Quaternion.identity);
clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed));
Destroy (clone.gameObject, 3);
}
for (i = 0; i < numberOfObjects; i++)
{
angle = i * Mathf.PI * 2 / numberOfObjects;
pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
clone = Instantiate(prefab, pos, Quaternion.identity);
clone.velocity = transform.TransformDirection( Vector3 (1, 0, speed));
Destroy (clone.gameObject, 3);
}
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
LaunchingProjectile();
}
}
=====Old code above. Newer code below=========
var prefab : Rigidbody;//instantiated prefab for clone
var speed = 15;//speed that clones travel
var numberOfObjects = 15;//Used in Mathf calculation
var radius = 1;//used in MathF calculation
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
LaunchingProjectile();
}
}
function LaunchingProjectile()
{
for (i = 0; i < numberOfObjects; i++)
{
var angle = i * Mathf.PI * 2 / numberOfObjects;
var position = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
clone = Instantiate(prefab, position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed));
clone.transform.parent = transform;
Destroy (clone.gameObject, 3);
}
}
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
Answer by DaveA · Apr 05, 2011 at 12:36 AM
clone.transform.parent = transform; would be my guess. Assumes 'this' object the script is on is the parent?
clone = Instantiate(prefab, position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed));
clone.transform.parent = transform;
clone.transform.localPosition = position;
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
Actually, in your case, I think you'd set clone.transform.localPosition = pos;
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
You have to put them after, they need to have been created. Setting the position after creation should work.
Your answer
Follow this Question
Related Questions
JavaScript error BCE0005 0 Answers
Click to destroy object 2 Answers
Moving gameobject relative to resolution 1 Answer
Function OnTriggerEnder (difficult JavaScript) 1 Answer
Enemy AI Error 1 Answer