WasTabon
@WasTabon

Почему спавн объектов работает неправильно?

606af2ec31a9f142375975.png
Объекты всё время спавнит в этой области (это не нулевые координаты)
+ на скрине видно 3 зоны (в каждой по 4 точки, которые область спавна обозначают
Код

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

public class Works : MonoBehaviour
{
    public GameObject[] coloredPrefabs;
    public GameObject[] spawnZones;
    public GameObject shop;
    public Color32[] prefabColor;
    public SpriteRenderer spriteRendererSHOP;
    public Text textMoneyCourierEarn;
    public int howMuchEarn = 0;
    public int ci;
    public Transform[] firstZone;
    public Transform[] secondZone;
    public Transform[] ThirdZone;
    public string nameColor;
    System.Random r = new System.Random();

    private void Awake()
    {
        spriteRendererSHOP = shop.GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        StartCoroutine(CourierWorkShopColor());
        howMuchEarn = 0;
        textMoneyCourierEarn.text = "Вы заработали - " + howMuchEarn;
        for(int i = 0; i < 10; i++)
        {
            //System.Random r = new System.Random();
            int randZone = r.Next(0, 2);
            switch(randZone)
            {
                case 0:
                    float randPosY = Random.Range(firstZone[0].position.y, firstZone[1].position.y);
                    float randPosx = Random.Range(firstZone[2].position.x, firstZone[3].position.x);
                    Vector3 vector3 = new Vector3(randPosY, randPosx, 0);
                    Instantiate(coloredPrefabs[i], vector3, Quaternion.identity);
                    break;
                case 1:
                    float randPosY2 = Random.Range(secondZone[0].position.y, secondZone[1].position.y);
                    float randPosx2 = Random.Range(secondZone[2].position.x, secondZone[3].position.x);
                    Vector3 vector32 = new Vector3(randPosY2, randPosx2, 0);
                    Instantiate(coloredPrefabs[i], vector32, Quaternion.identity);
                    break;
                case 2:
                    float randPosY3 = Random.Range(ThirdZone[0].position.y, ThirdZone[1].position.y);
                    float randPosx3 = Random.Range(ThirdZone[2].position.x, ThirdZone[3].position.x);
                    Vector3 vector33 = new Vector3(randPosY3, randPosx3, 0);
                    Instantiate(coloredPrefabs[i], vector33, Quaternion.identity);
                    break;
            }    
        }
        StartCoroutine(CourierWorkShopSpawnGoods());
    }

    private void Update()
    {
        textMoneyCourierEarn.text = "Вы заработали - " + howMuchEarn;
    }

    public IEnumerator CourierWorkShopColor()
    {
        while (true)
        {
            System.Random r = new System.Random();
            ci = r.Next(0, coloredPrefabs.Length);
            nameColor = ci.ToString();
            spriteRendererSHOP.color = new Color32(prefabColor[ci].r, prefabColor[ci].g, prefabColor[ci].b, 255);
            yield return new WaitForSeconds(3.5f);
        }
    }
    public IEnumerator CourierWorkShopSpawnGoods()
    {
        while (true)
        {
            yield return new WaitForSeconds(3f);
            int randZone = r.Next(0, 2);
            int randPrefab = r.Next(0, 10);
            if (randZone == 0)
            {
                float randPosY = Random.Range(firstZone[0].position.y, firstZone[1].position.y);
                float randPosx = Random.Range(firstZone[2].position.x, firstZone[3].position.x);
                Vector3 vector3 = new Vector3(randPosY, randPosx, 0);
                Instantiate(coloredPrefabs[randPrefab], vector3, Quaternion.identity);
            }
            else if (randZone == 1)
            {
                float randPosY2 = Random.Range(secondZone[0].position.y, secondZone[1].position.y);
                float randPosx2 = Random.Range(secondZone[2].position.x, secondZone[3].position.x);
                Vector3 vector32 = new Vector3(randPosY2, randPosx2, 0);
                Instantiate(coloredPrefabs[randPrefab], vector32, Quaternion.identity);
            }
            else if (randZone == 2)
            {
                float randPosY3 = Random.Range(ThirdZone[0].position.y, ThirdZone[1].position.y);
                float randPosx3 = Random.Range(ThirdZone[2].position.x, ThirdZone[3].position.x);
                Vector3 vector33 = new Vector3(randPosY3, randPosx3, 0);
                Instantiate(coloredPrefabs[randPrefab], vector33, Quaternion.identity);
            }
        }
    }
}


Спойлер
Извиняюсь если уже задолбал со своими вопросами
  • Вопрос задан
  • 73 просмотра
Решения вопроса 1
@namee
на сколько я вижу оно из префаба спавнит.
Видимо там и установлены эти координаты.

А вообще - хорошим тоном считается инициализация переменных.
То есть после спавна сразу следует задавать координаты и предка.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы