我目前正在Unity中创建一个游戏,试图在实例化不同的生成点,与玩家互动的游戏对象将在该生成点处生成。该脚本的目标是确保每个实例化的派生点都在屏幕上方的X个不同位置处实例化,并且从一个到下一个相等的距离,而无论要实例化的派生点的数量如何。但是,当我尝试运行游戏时,什么也没有发生。出于测试目的,我暂时将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++;
}
}
}
}
}
}