Home > front end >  My characters in game spawn randomly and they get squashed together in one position
My characters in game spawn randomly and they get squashed together in one position

Time:08-09

My game characters keep spawning at one position and they get squashed together i want them to spawn one after another from the door of the shop and after they recieve their product they leave from the door again This is a picture of how they look when they spawn since i'm putting them in a group spawn:

Characters Spawn

Cat Spawn

This is the script i'm using to spawn them:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterSpawn : MonoBehaviour
{
    [SerializeField]
    private float spawnRadius = 7, time = 1.5f;

    public GameObject[] enemies;
    public Transform groupTransform;




    void Start()
    {
        StartCoroutine(SpawnAnEnemy());
    }

    IEnumerator SpawnAnEnemy()
    {
        Vector2 spawnPos = GameObject.Find("Bengal").transform.position;
        

        Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity, groupTransform);
        yield return new WaitForSeconds(time);
        StartCoroutine(SpawnAnEnemy());

    }
}

EDIT: They still spawn together even after trying the solution bellow check this picture:

Squashed characters

CodePudding user response:

Add a float value called spaceBetweenObjects and add it to the spawnPos

//start with and small value and gradually increase or decrease it until you are happy with the result.
public float spaceBetweenObjects = 1;  
Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos   spaceBetweenObjects, Quaternion.identity, groupTransform);

CodePudding user response:

I think you will need two scripts for this. One to handle selecting enemy to spawn and another to handle where to place the enemy.

This will pick a random enemy prefab:

public GameObject[] enemies; //Add to enemy prefabs
int spawnEnemy; //Random enemy index

void Start()
{
    Pick();
}

void Pick()
{
    spawnEnemy = Random.Range(0, enemies.Length);
    GameObject Spawn = Instantiate(enemies[spawnEnemy], transform.position, Quaternion.identity);

This will place the enemy prefab in a random position. To avoid overlapping, a float "spaceBetweenEnemies" is introduced.

//Add to your enemy manager. Create an empty game object if you don't have one
//This will place the enemies in random position
public GameObject enemyDistribution;   
GameObject spawn;

public int numberOfEnemiesToSpawn;
public float spaceBetweenEnemies = 2; //This is the space between enemies. To avoid overlapping
public float xPos = 5; //x axis
public float yPos = 5; //y axis

void Start()
{
    for (int i = 0; i < numberOfEnemiesToSpawn; i  )
    {
        SpreadItem(); 
    }        
}

void SpreadItem()
{
    Vector3 ranPos = new Vector3(Random.Range(-xPos, xPos)   spaceBetweenEnemies, Random.Range(-yPos, yPos)   spaceBetweenEnemies, 0f)   transform.position; //z axis is set to zero        
    spawn = Instantiate(enemyDistribution, ranPos, Quaternion.identity);          
} 

This may not prevent the enemies from spawning outside the screen parameters. I recommend writing a simple script to attract the enemies towards the centre of the screen of to the player. You can use an "OnTriggerEnter2d" method to check for collision between enemies and to move away from each other. There are good tutorials on "Flocking" on YouTube. You enemies will behave like swarms. This might come in handy for your game.

Hope this helps.

  • Related