Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 DedValve · Aug 09, 2015 at 04:13 AM · instantiateaxislength

Instantiating objects randomly based on the length of an object

Searched everywhere and tried various ways but I don't believe I am able to do this. I want to make a group of 3 asteroids (that are randomly selected) spawn based on the length of an empty gameobject (I am using box collider to determine length). I currently have it based on random z or x axis but those are creating some wonky things and making it so its based on the length of a particular axis of a collider sounds much easier and more automated.

The setup I have now is an empty gameobject called "Asteroid Generator" and a child (that has this script and does the spawning) called "Asteroid Spawner"

I also plan to have another child within generator that will be the same box size that will end up deleting all asteroids that pass as well as a timer that will randomly spawn the asteroids rather than spawn infinite asteroids at once.

Here's what I have:

 #pragma strict
 
 var asteroids = new GameObject[3];
 var pos1: float;
 var pos2: float;
 
 
 
 function Start () {
 }
 
 function Update () {
 //    for (var child : Transform in transform) {
         for(var i : int = 0; i < asteroids.length; i++)
         {
             var randomPosition = Vector3(transform.position.x, 0, Random.Range(pos1, pos2)); //randomly spawns asteroids across z axis (done here as forloops update here)
             var thingToSpawn : int = Random.Range( 0, asteroids.length ); //chooses randomly diferrent asteroids
             var setAsteroid = Instantiate( asteroids[thingToSpawn], randomPosition, transform.rotation ); //instantiates selected asteroid 
             
             setAsteroid.transform.parent = transform;
             Debug.Log("instatiating");
         }
 //    }
 }
Comment
Add comment · Show 10
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 allenallenallen · Aug 09, 2015 at 04:15 AM 1
Share

What do you mean by the length of the object? The X, Y or Z axis?

avatar image sparkzbarca · Aug 09, 2015 at 04:20 AM 1
Share

are you just trying to get the length of a collider or?

collider.bounds.x / y/ z will get you that.

avatar image DedValve · Aug 09, 2015 at 02:44 PM 0
Share

Im trying to get the z-Axis of an object. So if an object has a very long z-axis then the asteroids should spawn all across that z-axis. If it has a short z-axis then it only spawns within that.

So would collider.bounds.xyz work in a vector3 or instantiate line? I'll experiment with that and post results.

avatar image allenallenallen · Aug 09, 2015 at 04:19 PM 1
Share
 GetComponent.<Collider>().bounds.size.z

API here: http://docs.unity3d.com/ScriptReference/Bounds-size.html

avatar image allenallenallen · Aug 10, 2015 at 03:23 AM 1
Share

Well yeah, we guessed that you have one end of the collider at zero so that our code would work.

Obviously if one end of your collider isn't at zero, there will be offsets.

 Random.Range(zOffset, zOffset + GetComponent<Collider>().bounds.size.z)

zOffset will deter$$anonymous$$e the starting point. You can find this value by looking at the Z axis, scale and maybe doing some math.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

prevent object from falling to the left side when instantiated (spawned) ? 1 Answer

Instantiate objects at random positions on axis 2 Answers

Checking if object intersects? 1 Answer

Instantiate a rotated object 4 Answers

Help needed on getting distance value of chain of links after instantiation 0 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