繁体   English   中英

Unity C#脚本,Vector3不更新

[英]Unity C# Script, Vector3 not updating

这是我的代码,brick_col正在更新自身,print(brick_col),告诉我循环完成后brick_col本身就是+1,但是print(positions [i])告诉我,我的y值始终为0 )不会使用该值更新Vector3。 有任何想法吗? 非常感谢

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

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;
private int brick_col=0;
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();

}


void Check_Bricks(){
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();


    }


} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++)
    {
        for (int a = -4; a <= no_in_row/2; a++)
        {
            positions.Add(new Vector3(a,brick_col,0f));
        }
        print (brick_col);
        print (positions [i]);
        transform.position = positions[i];
        Instantiate(Brick,transform.position, transform.rotation);

    }
    brick_col = brick_col + 1;
    Check_Bricks ();
}

}

在您的代码中,将以下变量用作y值

private int brick_col=0;

在内部循环中,您可以使用以下命令将元素添加到您的职位列表中

positions.Add(new Vector3(a,brick_col,0f));

除非您都处于两个循环之外,否则不更新brick_col。

移动这个brick_col = brick_col + 1; 到您希望更新真正发生的位置,如果将其放入内部循环,则可能还需要在再次输入之前将其重置。

好吧,老实说,您正在做一些不必要的事情,我将解释为什么当我查看它时,有时在我试图弄清正在发生的事情时,或者当我急于建造某些东西时也会这样做。我很高兴尝试一下,因此一开始我将使用您的代码并进行说明,然后解决该问题,然后我将展示另一种方法:

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

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;  // No need for a number in row because you have bricks in row which is the same thing.
private int brick_col=0;  // No need for this variable, as you are going to be counting anyways (in this case your i variable)
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();
}


void Check_Bricks(){  // This function is unnessary, it appears it may have been added when you were trying to fix your y issue.
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();
    }
} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++) // This will run 9 times.
    {
        for (int a = -4; a <= no_in_row/2; a++) // This will also run 9 times
        {
            positions.Add(new Vector3(a,brick_col,0f));
        }
        // Move all this into the inner loop.
        print (brick_col);
        print (positions [i]); // By this point you will have 9 then 18 then 27... as your inner loop this position would be positons[i * bricks_in_row + (a +4)] with how you are looping
        transform.position = positions[i]; /// This positions should be based off of the individual brick, next time around you are setting this position to the second index but by this time you have 18.
        Instantiate(Brick,transform.position, transform.rotation);
        // 
        // brick_col = brick_col + 1; This will be in the outter loop  
    }
    brick_col = brick_col + 1; // This should be before the closing bracket. not outside the loop
    Check_Bricks ();
}

}

如果我保留您的变量并仅解决了y和定位问题,则外观将如下所示:

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

public class Brick_Spawn_Test : MonoBehaviour {

List<Vector3> positions = new List<Vector3>();
private int bricks_in_row=9;
public GameObject Brick;
private int no_in_row=9;  
private int brick_col=0;  
private int number_of_brick_col=2;

void Start(){
    Check_Bricks ();
}


void Check_Bricks(){ 
    if (brick_col != number_of_brick_col) {
        print ("not enough bricks");
        Create_Bricks ();
    }
} 


void Create_Bricks(){
    for (int i = 0; i <= bricks_in_row-1; i++) 
    {
        for (int a = -4; a <= no_in_row/2; a++) 
        {
            positions.Add(new Vector3(a,brick_col,0f));
            print (brick_col);
            print (positions [i * bricks_in_row + a +4]);
            transform.position = positions[i * bricks_in_row + a +4];
            Instantiate(Brick,transform.position, transform.rotation);
        }
        brick_col = brick_col + 1; 
    }
    Check_Bricks ();
}

}

这是一种处理方式,您可以忽略我为变量命名的方式,因为这是优先选择的问题:

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

public class Brick_Spawn_Test : MonoBehaviour {
    [SerializeField]
    private int totalBrickColumns = 9; // Serializing it so I can use this for multiple test cases and edit the variable in the inpsector.
    [SerializeField]
    private int totalBrickRows = 2;
    [SerializeField]
    private Vector2 startingPoint = new Vector2(-4, 0);
    [SerializeField]
    private GameObject Brick;

    void Start()
    {
        CreateBricks();
    }

    void CreateBricks()
    {
       Vector2 spawnPosition = startingPoint;

       for(int x = 0; x < totalBrickColumns; ++x) // x is my column 
       {
           spawnPosition.x = startingPoint.x + x;  // the x is my offset from the startingPoint.x so if I wanted to start at -4 i just set the startingPoint.x to -4
           for(int y = 0; y < totalBrickColums; ++y) // y is my row
           { 
               spawnPosition.y = startingPoint.y + y; // the y is my offset from the startingPoint.y
               print("Brick Location: " + spawnPosition.toString());
               Instantiate(Brick,spawnPosition, transform.rotation);
           }
        }
    }
}

关于为什么您的y不更新,是因为您没有在第一个循环内更新变量。 请参阅Create_Brick()函数中关于brick_col的代码中的Create_Brick()

编辑:当我说你需要更新外循环时,我注意到我没有考虑的事情,我还只使用了代码和变量来添加了修复程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM