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 SamMarchand · Dec 06, 2017 at 05:48 PM · generationrandomspawningasteroids

Random generation of random asteroids

Hi all,

I need to write a script to populate a scene with randomly generated asteroids for a 3rd person 3D game. This script should be able to choose from 6 models and add various components like rigidbody, collider and force. It should also assign random size/mass values and random force values (both within a range). I will reuse/modify this script to place power-ups in the scene.

I know basic C# scripting and I'm willing to learn. I use Unity2017/C# and know very little of Java.

Thanks for any insight/code.

Comment
Add comment · Show 1
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 SamMarchand · Dec 08, 2017 at 01:27 AM 0
Share

Here's the code I've written to achieve this goal. I've been revising this post as I've added key elements. Namely, it now does assign random size, mass and force to the asteroids. The trick was apparently to make my instantiated prefabs also GameObjects and then to use arrays to modify components values of each clone.

The values chosen suit my project but you can change them for yours. If you see anything that could be improved, let me know.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Asteroides : $$anonymous$$onoBehaviour {
 
     // Instantiates prefabs in boxed terrain.
 
     public GameObject[] Asteroid;
     private int nombreDobjets;
     private int index;
     private Rigidbody[] rb;
     private ConstantForce[] cf;
 
     void Start()
     {
         nombreDobjets = Random.Range(888, 1234);
         Vector3 TailleTerrain = GameObject.Find("Terrain").GetComponent<Terrain>().terrainData.size;
         rb = new Rigidbody[nombreDobjets];
         cf = new ConstantForce[nombreDobjets];
 
         for (int i = 0; i < nombreDobjets; i++)
         {
             index = Random.Range(0, 6);
 
             Vector3 pos = new Vector3(Random.Range(0, TailleTerrain.x), Random.Range(0, TailleTerrain.y), Random.Range(0, TailleTerrain.z));
 
             GameObject go = Instantiate(Asteroid[index], pos, Quaternion.identity) as GameObject;
 
             go.AddComponent<Rigidbody>();
             go.AddComponent<CapsuleCollider>();
             go.AddComponent<ConstantForce>();
             go.transform.localScale += new Vector3(Random.Range(0, 2f), Random.Range(0, 2f), Random.Range(0, 2f));
 
             rb[i] = go.GetComponent<Rigidbody>();
             cf[i] = go.GetComponent<ConstantForce>();
             rb[i].mass = Random.Range(345, 1000);
             rb[i].AddForce(Random.Range(0, 23), Random.Range(0, 23), 0, Force$$anonymous$$ode.Acceleration);
             rb[i].AddTorque(Random.Range(0, 23), Random.Range(0, 23), Random.Range(0, 23), Force$$anonymous$$ode.Acceleration);
             cf[i].force = new Vector3(0,188,0);
         }
     }

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ShadyProductions · Dec 07, 2017 at 08:04 PM

Here is the basic concept:

 using UnityEngine;

 public class AsteroidHandler : MonoBehaviour
 {
     [SerializeField]
     private GameObject[] AsteroidPrefabs;
 
     void Start()
     {
          SpawnAsteroids(100);
     }
     
     private void SpawnAsteroids(int amount) 
     {
          for (int i=0; i < amount; i++) 
          {
               var pos = new Vector3(Random.Range(-150, 150), Random.Range(-150, 150), Random.Range(-150, 150));
               var chosenAsteroid = AsteroidPrefabs[Random.Range(0, AsteroidPrefabs.Length)];
               var asteroid = Instantiate(chosenAsteroid , pos, Quaternion.identity);
 
               // You can still manipulate asteroid afterwards like .AddComponent etc
          }
     }
 }
Comment
Add comment · Show 1 · 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 Soulice · Nov 19, 2018 at 01:34 PM 0
Share

Worked very well. Whipped up a quick 'roid in Blender, dropped it and this script into Unity and voila.... asteroid field.

avatar image
0

Answer by APSchmidt · Dec 06, 2017 at 09:24 PM

This tutorial will teach you how to do that: https://unity3d.com/learn/tutorials/s/space-shooter-tutorial

Comment
Add comment · Show 1 · 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 SamMarchand · Dec 07, 2017 at 01:54 PM 0
Share

I've seen this tutorial and went thru the relevant section (Creating Hazards and Spawning Waves). It doesnt go as far I need but it had some useful code parts.

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

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random generation of Asteroids 3 Answers

Script for Procedural Generation 0 Answers

[2D] Random Terrain Generator 0 Answers

Multiple spawn location with random game object in random time spawn c# 1 Answer

Objects move down from top of screen,I want objects to randomly spawn at the top 2 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