Создание точек появления в разных позициях в зависимости от количества точек появления? - PullRequest
1 голос
/ 09 марта 2019

В настоящее время я создаю игру в Unity, в которой я пытаюсь создать различные точки появления, в которых будут появляться игровые объекты, с которыми мой игрок будет взаимодействовать. Цель сценария - убедиться, что экземпляры spawnpoint, которые создаются каждый, создаются в разных X-позициях над экраном с равным расстоянием от одного до следующего независимо от количества точек spawntitiated. Однако, когда я пытаюсь запустить свою игру, ничего не происходит. В целях тестирования я временно присвоил значение 4 размеру массива Transform, но это изменит последний, чтобы иметь возможность хранить любое значение. Кто-нибудь знает, что я делаю не так? Вот мой код:

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


public class BallSpawnerControl2 : MonoBehaviour
{
    public Transform[] spawnPoints = new Transform [4];
    public GameObject[] interact;
    int SpawnPoint, Interact;
    int index = 1;
    public static bool spawnAllowed;



    // Use this for initialization
    void Start()
    {


        spawnAllowed = true;
        InvokeRepeating("SpawnAInteract", 0f, 1f);
    }

    void SpawnAInteract()
    {
        Vector2 pos = Camera.main.WorldToViewportPoint(transform.position);
        double TotaLBalls = spawnPoints.Length * 1.8f; // Total length of all balls at same X/Y position 
        double TotalLspaces = 1.0f - TotaLBalls; // Length left for spaces
        int Nspaces = spawnPoints.Length + 1; // Nr of spaces 
        double Length1space = TotalLspaces / Nspaces;


        for (int d = 0; d < spawnPoints.Length; d++)
        {
            Vector3 SpawnPos = spawnPoints[d].position;
            double spawnPosX = 0.0 + Length1space * d;
            SpawnPos.x = (float)spawnPosX;
            pos.x = SpawnPos.x;
            pos.y = 1.3f;

            Instantiate(spawnPoints[d], pos,
                       Quaternion.identity);


        }

        if (spawnAllowed)
        {

            { 
                   for (int i = 0; i < spawnPoints.Length; i++)
                   {
                    if (index % 100 != 0)
                    {
                        Interact = 1;
                        Instantiate(interact[Interact], pos,
                        Quaternion.identity);
                     index++;
                    }
                    else
                    {
                        Interact = 0;
                        Instantiate(interact[Interact], pos,
                        Quaternion.identity);
                        index++;
                    }
                   }
            }

        }

    }

}

Отредактированная версия:

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


public class BallSpawnerControl2 : MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject[] interact;
    int SpawnPoint, Interact;
    int index = 1;
    public static bool spawnAllowed;
    //

    // Use this for initialization
    void Start()
    {
        Vector2 pos = Camera.main.WorldToViewportPoint(transform.position);
        double TotaLBalls = spawnPoints.Length * 1.8f; // Total length of all balls at same X/Y position 
        double TotalLspaces = 1.0f - TotaLBalls; // Length left for spaces
        int Nspaces = spawnPoints.Length + 1; // Nr of spaces 
        double Length1space = TotalLspaces / Nspaces; // 



        //double XPosition = 0.0 + Length1space * i;
        //Use variable position to assign positions to SpawnPoints based on value of ScoreValue and use position value to influence the movement of balls
        if (ScoreScript.scoreValue < 5)
        {
            Transform[] spawnPoints = new Transform[4];

            for (int d = 0; d < spawnPoints.Length ; d++)
            {

                // Use instantiate to duplicate spawnpoints
                Vector3 SpawnPos = spawnPoints[d].position;
                double spawnPosX = 0.0 + Length1space * d;
                SpawnPos.x = (float)spawnPosX;
                pos.x = SpawnPos.x;
                pos.y = 1.3f;

                Instantiate(spawnPoints[d], pos,
                           Quaternion.identity);



            }

        }


        /*  if (pos.x < 0.0) Debug.Log("I am left of the camera's view.");
      if (1.0 < pos.x) Debug.Log("I am right of the camera's view.");
      if (pos.y < 0.0) Debug.Log("I am below the camera's view.");
      if (1.0 < pos.y) Debug.Log(pos); */

        spawnAllowed = true;
        InvokeRepeating("SpawnAInteract", 0f, 1f);
    }

    void SpawnAInteract()
    {


        if (spawnAllowed)
        {

            { 
                   for (int i = 0; i < spawnPoints.Length; i++)
                   {
                    if (index % 100 != 0)
                    {
                        Interact = 1;
                        Instantiate(interact[Interact], spawnPoints[i].position,
                        Quaternion.identity);
                     index++;
                    }
                    else
                    {
                        Interact = 0;
                        Instantiate(interact[Interact], spawnPoints[i].position,
                        Quaternion.identity);
                        index++;
                    }
                   }
            }

        }

    }

}
...