我想在生成的预制件之间添加空间

问题描述 投票:0回答:1

我正在从事unity2D游戏并正在实例化预制件,以一种很好的方式来生成我的预制件,但是问题是我希望在它们生成时在它们之间留一个空间,我的意思是当预制件正在生成时我想在它们之间增加空间它们在位置x,有我的代码:

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);

          }

    } 
}

有什么帮助吗?请注意,我的预制件生成正确,问题仅在于位置x

c# unity3d
1个回答
0
投票

在我看来,您的代码只会从您的GameObject数组之一生成单个预制件。它的作用是:

  • 看一下CharacSpeed
  • 基于CharacSpeed,从easyWaves,mediumWaves或hardWaves实例化一个随机选择的GameObject。

所以,我不确定您将预制件间隔开是什么意思,因为只产生了一个预制件。

尝试一下:

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);
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.