Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Ghachii · Sep 28, 2011 at 12:26 AM · javascriptinstantiateprefabduplicateradius

Scripting a prefab to duplicate itself within a fixed radius

I'm trying to do something very simple with JavaScript, but cobbling together a script from the samples in the reference documentation isn't working for me at all.

I have a cylinder prefab, and all I want it to do is duplicate itself in a random position, within a certain radius. That cylinder would then also duplicate itself, etc, into infinity. Idealy, I'd also like to be able to adjust the number of duplicates that are instantiated.

I guess the first thing to do is to make the cylinder duplicate itself at all. This is all I have so far:

 var cylinder : GameObject;

function Update () {

 yield WaitForSeconds (3);
 cylinder = Instantiate(transform.position, transform.rotation);

}

Basically, I don't understand the syntax for 'Instantiate'.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by save · Sep 28, 2011 at 12:37 AM

Here's how you can work with instantiation:

 var myPrefab : GameObject;
 Instantiate(myPrefab, Vector3.zero, Quaternion.identity);

In the script above we instantiate a predefined object which is set to the variable myPrefab in the Inspector. The position is 0 on world-XYZ and the rotation is unchanged.

 var myPrefab : GameObject;
 var myGameObject = Instantiate(myPrefab, Vector3.zero, Quaternion.identity);

In this script we reference the prefab instantiation to myGameObject. The object will still instantiate as normal but will be able to be easily modified directly and referenced to. For instance:

 myGameObject.name = "Instantiated Prefab";

You always define like this: Instantiate(the object to spawn, the position, the rotation);

To instantiate something at a random position try this out:

 var rndX : int = Random.Range(-10, 10);
 var rndY : int = Random.Range(-10, 10);
 var rndZ : int = Random.Range(-10, 10);
 Instantiate(myPrefab, transform.position+Vector3(rndX, rndY, rndZ), Quaternion.identity);

Keep in mind that transform.position means the scripts current transform's position in world-space.

Comment
Add comment · 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
0

Answer by syclamoth · Sep 28, 2011 at 12:42 AM

Working version of your code, because explaining it is harder than demonstrating-

var circleRadius : float; var waitTime : float;

function Start() { StartCoroutine(WaitAndDuplicate()); }

function WaitAndDuplicate() { while(true) { yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(gameObject, transform.position + randomOffset, transform.rotation); } }

Add this script to the gameObject you want turned into a serious grey-goo scenario, and watch your GPU melt!

Instantiate takes 3 things- an original, a location, and a rotation. It copies the original, and puts it where you tell it to with the other two parts! As for the circle thing, Random.insideUnitCircle is a nice way of making random offsets in 2D- I had to rotate it back to the normal plane (in which x and z are the main coordinates and y is height, a decision I may never understand), so that it would spread out instead of up and down.

If you want your object to spawn anywhere in space (not just in a circle), replace

Random.insideUnitCircle

with

Random.insideUnitSphere

still multiplying this with the desired maximum radius (so that it is not just a number between Vector3.zero and one in any direction).

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Combining Meshes? 1 Answer

Bullet Instantiation 1 Answer

How to access script variables attached to a prefab at runtime in Javascript? 2 Answers

Storing and using weapons and objects 0 Answers

Prevent instantiating on collider 1 Answer


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