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 /
This question was closed Jun 22, 2018 at 04:30 PM by Noanii for the following reason:

Andere

avatar image
0
Question by Noanii · Jun 22, 2018 at 03:36 PM · c#objectgenerationalgorithmchunks

(c# source request) how to spawn a few bottom objects in a spiral to create a simple terrain?

hey dear community!

i want to create an automatic object spawner, wich will place object plates to create a ground. im now currently working on the solution for about 3 days but i dont get it to work(im super new to unity) however i already got a simple spawner script to work, wich will place one object at the location of a empty game object:) but i dont get how to place objects at fixed or calculated locations. my current script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawn : MonoBehaviour {
     public GameObject ObjToSpawn;
 
     // Use this for initialization
     void Start () {
         Instantiate(ObjToSpawn, transform.position, transform.rotation);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

![alt text][1]

Anyways my goal is to create a script wich will be able to generate a pattern like on the picture i want to be able to set the amount of placed objects and to add a list like 3 different objects to let the generator script choose from to generate more random:)

could someone help me?:)

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 LeeroyLin · Jun 22, 2018 at 04:53 PM 0
Share

A script write for you, hope this can help you.

 using UnityEngine;
 public class Spawn : $$anonymous$$onoBehaviour {
     public int amount = 8;
     public float spacing = 1;
     public GameObject[] objsToSpawn;
     
     int count = 1;
     Vector3 lastPos;
     Vector3 dir = new Vector3(-1, 0, 0);
     
     void Start()
     {
         if (amount <= 0)
             return;
 
         GameObject obj = Instantiate(objsToSpawn[Random.Range(0, objsToSpawn.Length)], transform.position, transform.rotation);
         lastPos = obj.transform.position;
         
         if (amount <= 1)
             return;
 
         bool isContinue;
         do
         {
             isContinue = GenerateOutsideObjs();
         } while (isContinue);
     }
 
     bool GenerateOutsideObjs()
     {
         GameObject obj;
         int generatedNum = 0;
         for (int i = 0; i < 2; i++)
         {
             for (int t = 0; t < count; t++)
             {
                 generatedNum = (count - 1) * count + count * i + t;
                 if (generatedNum == amount - 1)
                     return false;
 
                 obj = Instantiate(objsToSpawn[Random.Range(0, objsToSpawn.Length)], lastPos + dir * spacing , transform.rotation);
                 lastPos = obj.transform.position;
             }
             
             dir = Quaternion.Euler(0, 90, 0) * dir;
         }
         count++;
 
         return true;
     }
 }

1 Reply

  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Jun 22, 2018 at 04:01 PM

Good day.

It simple, you only need to change the transform.position every time. Create 2 variables, one for XCoord, another for Zcoord. Then do the Instantiate replacing the position for this vector.

 Position = new Vector3 (X, 0, Z);
 Position = new Vector3 (X+200, 0, Z);
 Position = new Vector3 (X+200, 0, Z+200);
 Position = new Vector3 (X, 0, Z+200);
 Position = new Vector3 (X-200, 0, Z+200);
 Position = new Vector3 (X-200, 0, Z);
 Position = new Vector3 (X-200, 0, Z-200);
 Position = new Vector3 (X, 0, Z-200);
 Position = new Vector3 (X+200, 0, Z-200);
 Position = new Vector3 (X,+400 0, Z-200);
 Position = new Vector3 (X+400, 0, Z);

etc...

If you are smatr, you can easy create an automatic progression, if notm, do it by hand.

Bye!

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 Noanii · Jun 22, 2018 at 04:16 PM 0
Share

thanks for your help just one question left; currently i use this code to spawn an object Instantiate(ObjToSpawn, transform.position, transform.rotation); how can i add x,y,z coordinates to it?

Follow this Question

Answers Answers and Comments

90 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 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

Distribute terrain in zones 3 Answers

Is there a way to load premade terrain chunks randomly? 1 Answer

Multiple Cars not working 1 Answer

Deleting one object, not all objects at once. 1 Answer

Is possible to use UISprite object of NGUI with event type 2D to detect intersection? 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