• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Musclegunner · Apr 05, 2011 at 12:15 AM · javascriptpositionlocaljavascript-specific

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);
    }
}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Musclegunner · Apr 05, 2011 at 12:17 AM 0
Share

First off, sorry about the formatting I'm having trouble trying to paste a code block but I'll figure out in due time

avatar image DaveA · Apr 05, 2011 at 12:34 AM 0
Share

Select code, hit the 'code' button on the toolbar, looks like 1's and 0's

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

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.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Musclegunner · Apr 05, 2011 at 12:57 AM 0
Share

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

avatar image DaveA · Apr 05, 2011 at 01:28 AM 0
Share

clone.transform.localPosition = Vector3.zero;

avatar image DaveA · Apr 05, 2011 at 01:29 AM 0
Share

Actually, in your case, I think you'd set clone.transform.localPosition = pos;

avatar image Musclegunner · Apr 05, 2011 at 04:14 AM 0
Share

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

avatar image DaveA · Apr 05, 2011 at 04:23 AM 0
Share

You have to put them after, they need to have been created. Setting the position after creation should work.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

JavaScript error BCE0005 0 Answers

Drag Script Not Working 1 Answer

Accessing another script from a class 3 Answers

Move from point A to point B, then destroy? ( java S) 2 Answers

Loading next level, but next level never gets incremented. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges