Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在简单场景中以随机位置实例化一些立方体_C#_Unity3d - Fatal编程技术网

C# 在简单场景中以随机位置实例化一些立方体

C# 在简单场景中以随机位置实例化一些立方体,c#,unity3d,C#,Unity3d,我是C#的新手,我正在unity3D上构建一个Braitenberg车辆仿真。我正在创建一个环境,随机定位的立方体 问题1:它创建的立方体数量是我插入创建的立方体数量的两倍 问题2:我很难让车辆避开立方体。我开始尝试在控制台中打印一条消息,因为车辆靠近一个立方体,但它不起作用 下面是我创建多维数据集的代码 public class Cubes : MonoBehaviour { public float distance; public List<GameObject> allCub

我是C#的新手,我正在unity3D上构建一个Braitenberg车辆仿真。我正在创建一个环境,随机定位的立方体

问题1:它创建的立方体数量是我插入创建的立方体数量的两倍

问题2:我很难让车辆避开立方体。我开始尝试在控制台中打印一条消息,因为车辆靠近一个立方体,但它不起作用

下面是我创建多维数据集的代码

public class Cubes : MonoBehaviour {
public float distance;
public List<GameObject> allCubes = new List<GameObject>();
public float[,] positionS = new float[10, 2];
void Start () {
    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    for (int x = 0; x < 10; x++)
    {
        positionS[x, 0] = Random.Range(-20F, 20F);
        positionS[x, 1] = Random.Range(-20F, 20F);

        //cube.AddComponent<Rigidbody>();
        //cube.transform.position = new Vector3(position[x,0], position[x,1], 0.5F);

        allCubes.Add((GameObject)Instantiate(cube, new Vector3(positionS[x, 0], 0.5F, positionS[x, 1]), Quaternion.identity));  
    }
}    
公共类多维数据集:单行为{
公众浮标距离;
public List allCubes=new List();
公开浮动[,]头寸=新浮动[10,2];
无效开始(){
GameObject cube=GameObject.CreatePrimitive(PrimitiveType.cube);
对于(int x=0;x<10;x++)
{
位置[x,0]=随机范围(-20F,20F);
位置[x,1]=随机范围(-20F,20F);
//AddComponent();
//cube.transform.position=新矢量3(位置[x,0],位置[x,1],0.5F);
添加((游戏对象)实例化(立方体,新向量3(位置[x,0],0.5F,位置[x,1]),四元数.identity);
}
}    
}

这里是我检查车辆之间距离的地方:

public class CubeDetector: Cubes
{
public float[] distances;
public CarBehaviour car;
void Update()
{
    for (int i = 0; i < allCubes.Count; i++)
    {
        distances[i] = Vector3.Distance(car.transform.position, allCubes[i].transform.position);

        if (i < 9)
        {
            if (distances[i] < distances[i++])
            {
                Console.WriteLine('y');
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine('n');
                Console.ReadLine();
            }
        }
    }
}
公共类CubeDetector:多维数据集
{
公共交通距离;
公共汽车驾驶汽车;
无效更新()
{
for(int i=0;i
}


欢迎任何帮助

你有很多问题,类名必须大写,所以Cubes,CubeDetector。谢谢,就像我说的,我是C#中的乞丐。你说我有很多错误,所以,除了你刚才写的那一个,你还看到其他的吗?