- Home /
Andere
(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?:)
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;
}
}
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!
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?