i want to add space between my generated prefabs - c#

Im working on unity2D game and im working on instantiating prefabs, im generating my prefabs in a good way but the problem is that i want a space between them when they are generated, i meant when prefabs are generating i want to add space between them in position x, there is my code:
public class WaveSpawn : MonoBehaviour
{
public GameObject[] easyWaves;
public GameObject[] mediumWaves;
public GameObject[] hardWaves;
public GameObject Player;
// Start is called before the first frame update
void Start()
{
SpawnWave();
}
// Update is called once per frame
void Update()
{
}
public void SpawnWave()
{
float CharacSpeed = Player.GetComponent<charachter>().moveSpeed;
float spacing = 0.1f;
if(CharacSpeed < 11){
Instantiate(easyWaves[Random.Range(0,easyWaves.Length)], new Vector3(gameObject.transform.position.x, gameObject.transform.position.y,0) , Quaternion.identity);
}
else if(CharacSpeed >= 11 && CharacSpeed < 14){
Instantiate(mediumWaves[Random.Range(0,mediumWaves.Length)], new Vector3(gameObject.transform.position.x , gameObject.transform.position.y,0), Quaternion.identity);
}
else if(CharacSpeed >= 14){
Instantiate(hardWaves[Random.Range(0,hardWaves.Length)], new Vector3(gameObject.transform.position.x, gameObject.transform.position.y,0), Quaternion.identity);
}
}
}
any help please? notice that my prefabs are generating correctly, the problem is only on position x

It looks to me like your code will only spawn a single prefab from one of your GameObject arrays. What it does is:
Look at the CharacSpeed
Based on the CharacSpeed, instantiate a single randomly selected GameObject from either easyWaves, mediumWaves or hardWaves.
So, I'm not sure what you mean by spacing your prefabs since there is only one prefab spawned.
Try this:
public void SpawnWave()
{
GameObject[] selectedWaves = new GameObject[];
float CharacSpeed = Player.GetComponent<charachter>().moveSpeed;
float spacing = 0.1f;
if(CharacSpeed < 11){
selectedWaves = easyWaves;
}
else if(CharacSpeed >= 11 && CharacSpeed < 14){
selectedWaves = mediumWaves;
}
else if(CharacSpeed >= 14)
{
selectedWaves = hardWaves;
}
for (int i = 0; i < selectedWaves.Length; i++) {
Instantiate(selectedWaves[Random.Range(0,selectedWaves.Length)], new Vector3(gameObject.transform.position.x + (spacing * i), gameObject.transform.position.y,0) , Quaternion.identity);
}
}
You should then be able to adjust the amount of spacing by changing the float spacing = 0.1f value.
This assumes that you want to spawn as many prefabs as are contained in your GameObject arrays (and may want to spawn the same one multiple times). If not, change selectedWaves.Length in the for loop to whatever number of prefabs you want spawned.

Related

How to change a variable in a shared script, without affecting all the other game objects that share that script?

Ive been at it all day trying to solve this, because I dont want to make a separate script for every bullet, instead I want to have a single EnemyBulletMovement script, that can move each bullet in a different way, depending on the BulletType int that I feed into it when I instantiate it.
The problem is that if I fire multiple bullets at the same time, they will all change BulletType as soon as another bullet is instantiated, because they are all sharing the same script.
My goal is to have all bullets have a private script, but no matter what I try the variable will still be changed for all active bullets every time I try to change it for just one of them.
EnemyController script that Instantiates the bullets and gives them a BulletType value:
GameObject enemyBullet = ObjectPooler.SharedInstance.GetPooledEnemyBullet();
if (enemyBullet != null)
{
Quaternion rotationAmount = Quaternion.Euler(0, 0, 0);
Quaternion postRotation = transform.rotation * rotationAmount;
enemyBullet.transform.position = transform.position;
enemyBullet.transform.rotation = postRotation;
enemyBullet.SetActive(true);
enemyBullet.GetComponent<EnemyBulletMovement>().SetBullet(2);
}
GameObject enemyBullet2 = ObjectPooler.SharedInstance.GetPooledEnemyBullet();
if (enemyBullet2 != null)
{
Quaternion rotationAmount = Quaternion.Euler(0, 3, 0);
Quaternion postRotation = transform.rotation * rotationAmount;
enemyBullet2.transform.position = transform.position;
enemyBullet2.transform.rotation = postRotation;
enemyBullet2.SetActive(true);
enemyBullet2.GetComponent<EnemyBulletMovement>().SetBullet(0);
}
Shared EnemyBulletMotion script, that gets the bulletType from the above script. This is the problem:
public void SetBullet(int typeIndex)
{
bulletType = typeIndex;
Debug.Log(bulletType);
}
private void BulletMotion()
{
if (bulletType == 0)
{
rb.velocity = pos;
}
if (bulletType == 1)
{
axis = Mathf.Sin(Time.deltaTime * -frequency) * magnitude * transform.up; // Up down Wave motion
transform.Rotate(Vector3.forward * sideMag); // Side Sway Motion (+ Wave = Spiral Motion)
rb.velocity = pos + axis; // Combine all Motions
}
if (bulletType == 2)
{
Debug.Log("Shootin");
axis = Mathf.Sin(Time.deltaTime * -frequency) * magnitude * transform.up; // Up down Wave motion
transform.Rotate(Vector3.forward * -sideMag); // Side Sway Motion (+ Wave = Spiral Motion)
rb.velocity = pos + axis; // Combine all Motions
}
}
EDIT: This Debug Shootin never appears in my console though, so maybe the bulletType is not being read correctly here after all? For Clarification, the first Debug.Log(bulletType); return a 0 or a 2 so it is working. But the second Debug.Log("Shootin"); does not get printed, ever.
I tried private, static, and made an Instance out of the whole EnemyBulletMovement script but nothing works.
When I debug this code, the script works, the bulletType will change from 2 to 0 and back, but when it does it will change both bullets, so both will fly the same way, when what I want is to have each bullet follow its own motion. If there is no way to do this I will have to create a separate script for each bulletmotion, but im only showing 2 here and I already have a lot of them, thats why I wanted to try this out and make it work with if statements and then use case statements later.
EDIT: Added ObjectPooler script below.
public List<GameObject> pooledEnemyBullets;
public GameObject EnemyBulletToPool;
public int EnemyBulletAmountToPool;
pooledEnemyBullets = new List<GameObject>();
public static ObjectPooler SharedInstance;
void Awake()
{
SharedInstance = this;
}
void Start()
{
for (int i = 0; i < EnemyBulletAmountToPool; i++)
{
GameObject obj3 = (GameObject)Instantiate(EnemyBulletToPool);
obj3.SetActive(false);
pooledEnemyBullets.Add(obj3);
}
}
public GameObject GetPooledEnemyBullet()
{
//1
for (int i = 0; i < pooledEnemyBullets.Count; i++)
{
//2
if (!pooledEnemyBullets[i].activeInHierarchy)
{
return pooledEnemyBullets[i];
}
}
//3
return null;
}
}
Bullet type should be declared as private int bulletType. If you declare bullet type as private static int bulletType it will be same for all bullets.
If it is already declared as private int bulletType, then you should check the logic where you use object pooler. You may be processing the same bullet over and over, or processing all bullets in the pool.

Checking collisions in near objects for random object spawning in Unity 5

I have almost finished my first game in Unity, which is a simple maze with characters collecting different coins. As it was a hassle to manually put all the different coins in the terrain, I decided to try and make a random generator for the coins.
The script is largely working, but the problem is that it's spawning objects most of the time inside walls. So I tried to add rigid bodies to coins, but this is messing with the coins position as the coins have a script for rotation. I also have the coins collision triggered because they perform a certain action when colliding with the character.
So after some research I saw that I need to check in my generator script for a near object collision and somehow change the spawn position of the coin if it's true. I'm trying to figure out how I can do that. My script is attached to my terrain and it looks like this:
using System.Collections;
using CustomArrayExtensions;
using UnityEngine;
public class ObjectSpawner : MonoBehaviour
{
[SerializeField] public GameObject[] letters;
GameObject selectedObject;
private int amount = 0;
public int limit;
void Start()
{
StartCoroutine(SpawnObjects());
}
IEnumerator SpawnObjects()
{
selectedObject = letters.GetRandom();
while (amount < limit)
{
Instantiate(selectedObject, new Vector3(Random.Range(35.0f, 950.0f), 13.0f, Random.Range(35.0f, 950.0f)), Quaternion.identity);
yield return new WaitForSeconds(5.0f);
amount++;
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Fox_Main")
{
Debug.Log("Collision Detected");
}
}
}
So i tried both answers and they didn't help me.Coins and other objects is still spawning close/inside the walls.My boxes have box collider and rigidbody with all positions/rotations locked(i need it that way so that i can destroy it later without moving it with the character),walls have box collider.So i tried to modify your solutions following Unity Documentation.I failed too.Here is what i have done so far.
See here
using System.Collections;
using UnityEngine;
public class RandomCrates : MonoBehaviour
{
public GameObject[] crates;
private GameObject selectedCrate;
public int limit = 0;
public float x, y, z;
public float forX_From,forX_To,forZ_From,forZ_To;
private int mask = 1 << 7;
void Start()
{
StartCoroutine(SpawnObjects());
}
IEnumerator SpawnObjects()
{
for (int i = 0; i < crates.Length; i++)
{
for (int j = 0; j < limit; j++)
{
Vector3 freePos = GetFreePosition();
Instantiate(crates[i], freePos, Quaternion.identity);
yield return new WaitForSeconds(0.0f);
}
}
}
Vector3 GetFreePosition()
{
Vector3 position;
Collider[] collisions = new Collider[1];
do
{
position = new Vector3(Random.Range(forX_From, forX_To), 10.0f, Random.Range(forZ_From, forZ_To));
} while (Physics.OverlapBoxNonAlloc(position, new Vector3(x, y, z), collisions, Quaternion.identity, mask) > 0);
return position;
}
}
Unity provides a simple way to detect collisions in specific position with Physics.OverlapSphere:
public class ObjectSpawner : MonoBehaviour
{
[SerializeField] public GameObject[] letters;
GameObject selectedObject;
private int amount = 0;
public int limit;
private float radius = 2f; //Radius of object to spawn
void Start()
{
StartCoroutine(SpawnObjects());
}
IEnumerator SpawnObjects()
{
selectedObject = letters[Random.Range(0, letters.Length)];
while (amount < limit)
{
Vector3 spawnPos = new Vector3(Random.Range(35.0f, 950.0f), 13.0f, Random.Range(35.0f, 950.0f));
//Check collisions
if (DetectCollisions(spawnPos) > 0)
continue;
Instantiate(selectedObject, spawnPos, Quaternion.identity);
yield return new WaitForSeconds(5.0f);
amount++;
}
}
private int DetectCollisions(Vector3 pos)
{
Collider[] hitColliders = Physics.OverlapSphere(pos, radius);
return hitColliders.Length;
}
}
In this way, if there is a collision in spawn position, the coin will not be spawned.
There are different ways of approaching this problem and given more information one could come up with a smarter approach. However, to provide you with a simple solution to your problem: You could just pick random positions until you find a "free spot".
Assuming your coins to have a size of 1, you could do something like:
IEnumerator SpawnObjects()
{
selectedObject = letters.GetRandom();
while(amount < limit)
{
Vector3 freePos = GetFreePosition();
Instantiate(selectedObject, freePos), Quaternion.identity);
yield return new WaitForSeconds(5.0f);
amount++;
}
}
Vector3 GetFreePosition()
{
Vector3 position;
Collider[] collisions = new Collider[1];
do
{
position = new Vector3(Random.Range(35.0f, 950.0f), 13.0f, Random.Range(35.0f, 950.0f));
}
while(Physics.OverlapSphereNonAlloc(position, 1f, collisions) > 0);
return position;
}
Please note that Physics.OverlapSphereNonAlloc() can also accept a LayerMask for filtering collisions based on layers. You might want to look into this, to prevent detecting collision with your terrain, which may well keep the do-while loop from exiting and freeze Unity.

Unity - Spawn Prefabs (Clone Object) got wrong position

I am trying to spawn prefabs (clone object) AGAIN with random position after it's SetActive(false).
What I want :
After Swimmer Object enter trigger with Prefabs (clone object),
set Prefabs (clone object) to SetActive(false) and then it must spawn in random position.
What I have done :
Swimmer.cs <-- This is make clone abject SetActive(false) when trigger
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Trash") {
other.gameObject.SetActive (false);
}
}
Trash.cs
public GameObject columnPrefab;
public int columnPoolSize = 5;
public float spawnRate = 3f;
public float columnMin = -1f;
public float columnMax = 3.5f;
private GameObject[] columns;
private int currentColumn = 0;
private Vector2 objectPoolPosition = new Vector2 (-15,-25);
private float spawnXPosition = 10f;
private float timeSinceLastSpawned;
void Start()
{
timeSinceLastSpawned = 0f;
columns = new GameObject[columnPoolSize];
for(int i = 0; i < columnPoolSize; i++)
{
columns [i] = (GameObject)Instantiate (columnPrefab, objectPoolPosition, Quaternion.identity);
}
}
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate) {
timeSinceLastSpawned = 0f;
float spawnYPosition = Random.Range (columnMin, columnMax);
// This part what I am using to set it active
columns [currentColumn].SetActive(true);
columns [currentColumn].transform.position = new Vector2 (spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize) {
currentColumn = 0;
}
}
}
What I have got :
Prefabs (Clone Object) succeed spawn but on wrong position (float on right)
You can take a look at this image
So, how to SetActive clone object and spawn it for random position? Thanks
There isn't actually a problem, and everything in your game is working like it should.
You have your Scene View scrolled out slightly further than your Game View. You can see that if you look at the green seaweed on the left of your screen. See how your Scene View shows more leaves?
The Scene View is purely for the Unity editor, and you can zoom and scroll around independently of where your camera is in your Game View. If you want to move the camera within your Game View, you have to either change the camera parameters on the Main Camera object in your Hierarchy, or you can update Camera.main through code.

Memory overload on for loop

I am making an object spawn script in which on the start of the script the spawning function is called and in it is a for loop which creates an object each iteration. It first picks a random X position for it and then checks if it is in a range of another prefabs coordinates so they don't spawn too close or worse, one in each other. If it is in the same coordinates as another prefab it will return 0 and this same goes out for the Z axis too. It also picks a random Y axis rotation so it doesn't all face the same direction. After this it spawns the prefab and sets it's coordinates and rotation after which it check if the coordinates in the X or Z axis are 0, and if any of those two are 0 it goes back one iteration and the last object to be spawned is destroyed so it doesn't flood. This works perfectly but when you want to set it to spawn too much objects it floods the RAM because there is nowhere to spawn more objects. I tried finding the highest X position and highest Z position and multiplying them, and setting them both to positive, and then dividing them by the space between the prefabs but this doesn't work as it sets it to a really really high number. How would you fix this?
Script:
using UnityEngine;
using System.Collections;
public class PrefabSpawner : MonoBehaviour {
public int amountOfPrefabs;
public int maxAmountOfPrefabs;
private int currentSpawnedPrefab;
public float spaceBetweenPrefabs;
private float positionX;
private float positionZ;
private float maxPositionX;
private float maxPositionZ;
private float multipliedPosXZ;
private bool previousSpawnHadZero;
public GameObject prefab;
private GameObject point1;
private GameObject point2;
private GameObject currentSpawn;
private Vector2[] positions;
void Start () {
currentSpawnedPrefab = 0;
previousSpawnHadZero = false;
point1 = gameObject.transform.GetChild (0).gameObject;
point2 = gameObject.transform.GetChild (1).gameObject;
if (point1.transform.position.x > point2.transform.position.x)
maxPositionX = point1.transform.position.x;
else
maxPositionX = point2.transform.position.x;
if (point1.transform.position.z > point2.transform.position.z)
maxPositionZ = point1.transform.position.z;
else
maxPositionZ = point2.transform.position.z;
multipliedPosXZ = maxPositionX * maxPositionZ;
if (multipliedPosXZ < 0)
multipliedPosXZ += multipliedPosXZ + multipliedPosXZ;
maxAmountOfPrefabs = Mathf.FloorToInt (multipliedPosXZ / spaceBetweenPrefabs);
if (amountOfPrefabs > maxAmountOfPrefabs)
amountOfPrefabs = maxAmountOfPrefabs;
point1.GetComponent<MeshRenderer> ().enabled = false;
point2.GetComponent<MeshRenderer> ().enabled = false;
gameObject.GetComponent<MeshRenderer> ().enabled = false;
positions = new Vector2[amountOfPrefabs];
SpawnPrefabs (amountOfPrefabs);
}
void SpawnPrefabs (int amount) {
for (int i = 0; i < amount; i++) {
if(previousSpawnHadZero)
i -= 1;
currentSpawn = (GameObject)Instantiate (prefab);
positionX = GetRandomPositionX ();
positionZ = GetRandomPositionZ ();
currentSpawn.transform.position = new Vector3 (positionX, this.transform.position.y + currentSpawn.transform.localScale.y, positionZ);
currentSpawnedPrefab += 1;
if (positionX == 0 || positionZ == 0) {
previousSpawnHadZero = true;
currentSpawnedPrefab -= 1;
Destroy (currentSpawn);
}
if (positionX != 0 && positionZ != 0) {
previousSpawnHadZero = false;
positionX = 0;
positionZ = 0;
}
}
}
IEnumerator Pause () {
yield return null;
}
float GetRandomPositionX () {
//Finds a random position for the X axis and then checks it and returns either 0 if the position is taken or the position if not
}
float GetRandomPositionZ () {
//Finds a random position for the Z axis and then checks it and returns either 0 if the position is taken or the position if not
}
bool CheckPositionAvailable (float pos, int axis) {
//Checks if the position is available.
}
}
Code is really long to debug but the problem is clearly visible and is from the SpawnPrefabs function. Currently, when you instantiate a prefab, you check if the generated position is 0. If 0, you subtract 1 from the i in the for loop then destroy the instantiated object and then start the for loop again from the current loop-1.
So the combination of Instantiate, Destroy and repeating it over again in the for loop is causing the memory issue.
What to do:
You have to re-write the whole function and this will require modification in your whole code too. Do not instantiate and destroy object in that loop unless when needed.
1.In the Start() function, create one prefab.
2.Make it to be invisible in the scene by disabling its mesh/sprite renderer.
3.Use that prefab in the for loop to check if the generated position is valid. If it is valid, you can now create/instantiate an object in the loop.
This prevents instantiating and destroying objects in the loop when you only create objects when if (positionX != 0 && positionZ != 0) .

Stuck with spawning objects in Unity

I'm making a 'run' game in Unity and I'm making a prototype with a ball, that has other balls following him. If the followers hit an object they get destroyed after some time. To make it so you don't run out of enemies I made a trigger that spawns new enemies. In the code this is the function Addzombies.
How do I make them spawn not on the same point, if i run it now they
start on eachother and bounce around as an explosion.
How do i make some start in the air, i tried it but they don't
spawn.
My code:
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float InputForce;
public GUIText guiText;
public float rotationHorizontal;
public AudioClip ACeffect2;
public GameObject zombiePrefab;
void FixedUpdate() {
rigidbody.AddForce( Camera.main.transform.right * Input.GetAxis("Horizontal") * InputForce);
rigidbody.AddForce( Camera.main.transform.forward * Input.GetAxis("Vertical") * InputForce);
transform.position += Vector3.forward *InputForce * Time.deltaTime;
rotationHorizontal = Input.GetAxis("Horizontal") * InputForce;
rotationHorizontal *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotationHorizontal);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Zombie") {
Debug.Log ("Player geraakt, nu ben je eigenlijk dood");
}
if (col.gameObject.name == "Obstakel1") {
Debug.Log ("Obstakel1 geraakt");
audio.PlayOneShot(ACeffect2);
InputForce = 0;
}
if (col.gameObject.name == "Obstakel2") {
Debug.Log ("Obstakel2 geraakt");
}
}
void AddZombies(int aantal){
for (int i = 0; i < aantal; i++){
GameObject go = GameObject.Instantiate(zombiePrefab, transform.position - new Vector3(0, 0, 7 + i),Quaternion.identity) as GameObject;
Zombie zb = go.GetComponent<Zombie>();
zb.target = gameObject.transform;
}
}
void OnTriggerEnter(Collider col) {
Debug.Log ("Enter" +col.name);
if (col.tag == "AddZombies"){
AddZombies(4);
}
}
void OnTriggerExit(Collider col) {
Debug.Log ("Leaving with" +col.name);
}
}
I will advice on how things could be done, but you will have to make changes to make it suit your requirement
public Transform zombiePrefab; // From the editor drag and drop your prefab
void addZombies()
{
// as you need them to be not on the same point
int randomX = Random.Range(-10.0F, 10.0F);
for (int i = 0; i < aantal; i++){
// make a transform
var zombieTransform = Instantiate(zombiePrefab) as Transform;
zombieTransform.position = new Vector3(randomX, 0, 7 + i);
transform.GetComponent<Rigidbody>().enabled = false;
// make it enable when you need and add force to make them fall
}
}
I would suggest passing the number of zombies to pass, and an integer representing the range of space that they can spawn in. Then just use UnityEngine.Random with said integer for each zombie to produce several different co-ordinates for spawning the zombies.
As for getting them to spawn in the air, just increase the y co-ordinate when you instantiate the zombie.

Categories

Resources