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,我有一个由16块瓷砖组成的网格。现在,基本上我希望用户通过在网格中的选择上随机移动来找到最终位置的路径 到目前为止,我成功地创建了用于向上、向下、向左和向右移动踏板的函数。当我试图用随机运动编码时,问题就出现了。理想情况下,这是一种设置方式,他不能离开网格边界 这就是我要做的: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Move : MonoBehav

我有一个由16块瓷砖组成的网格。现在,基本上我希望用户通过在网格中的选择上随机移动来找到最终位置的路径

到目前为止,我成功地创建了用于向上、向下、向左和向右移动踏板的函数。当我试图用随机运动编码时,问题就出现了。理想情况下,这是一种设置方式,他不能离开网格边界

这就是我要做的:

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


public class Move : MonoBehaviour

{
    void Up()
    {
        //get the Input from Horizontal axis
        float horizontalInput = Input.GetAxis("Horizontal");
        //get the Input from Vertical axis
        float verticalInput = Input.GetAxis("Vertical");

        //update the position
        transform.position = transform.position + new Vector3(0, 1.5f, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Down()
    {
        //get the Input from Horizontal axis
        float horizontalInput = Input.GetAxis("Horizontal");
        //get the Input from Vertical axis
        float verticalInput = Input.GetAxis("Vertical");

        //update the position
        transform.position = transform.position + new Vector3(0, -1.5f, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Left()
    {
        //get the Input from Horizontal axis
        float horizontalInput = Input.GetAxis("Horizontal");
        //get the Input from Vertical axis
        float verticalInput = Input.GetAxis("Vertical");

        //update the position
        transform.position = transform.position + new Vector3(-1.5f, 0, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Right()
    {
        //get the Input from Horizontal axis
        float horizontalInput = Input.GetAxis("Horizontal");
        //get the Input from Vertical axis
        float verticalInput = Input.GetAxis("Vertical");

        //update the position
        transform.position = transform.position + new Vector3(1.5f, 0, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }



    void Start()
    {
        var finalLocation = new Vector3(-0.5f, 0.5f, 0);
        var currentLocation = transform.position;

        while (currentLocation != finalLocation)
        {
            int randomNum = Random.Range(0, 3);

            if (randomNum == 0)
            {
                Up();
            }
            else if (randomNum == 1)
            {
                Down();
            }
            else if (randomNum == 2)
            {
                Left();
            }
            else if (randomNum == 3)
            {
                Right();
            }

        }
    }
}
更新的工作代码:

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


public class Move : MonoBehaviour

{
    void Up()
    {
        //update the position
        transform.position = transform.position + new Vector3(0, 1.5f, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Down()
    {
        //update the position
        transform.position = transform.position + new Vector3(0, -1.5f, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Left()
    {
        //update the position
        transform.position = transform.position + new Vector3(-1.5f, 0, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }

    void Right()
    {
        //update the position
        transform.position = transform.position + new Vector3(1.5f, 0, 0);

        //output to log the position change
        Debug.Log(transform.position);
    }



    void Update()
    {
        var finalLocation = new Vector3(2.5f, 2.0f, -2.0f);
        var currentLocation = transform.position; 
        int randomNum = Random.Range(0, 4);

        if (currentLocation != finalLocation) { 
                if (randomNum == 0)
            {
                Up();
            }
            else if (randomNum == 1)
            {
                Down();
            }
            else if (randomNum == 2)
            {
                Left();
            }
            else if (randomNum == 3)
            {
                Right();
            }
            return;
        }
    }
}
我的最后一个问题是如何限制这种随机性,使其只停留在网格上而不脱离网格。有什么想法吗?

几个问题:

  • 您的对象在循环时陷入了无尽的
    ,因为在随机移动脚本到达其
    最终分配位置之前,它不允许退出(并执行渲染帧、从用户处获取输入等操作)。您没有给游戏时间做任何其他事情。
    您几乎肯定希望这是一个协同程序或
    Update
    函数
  • 返回一个介于
    min
    (包含)到
    max
    (独占)之间的整数,因此对它的调用将永远不会返回
    3
  • 几个问题:

  • 您的对象在循环时陷入了无尽的
    ,因为在随机移动脚本到达其
    最终分配位置之前,它不允许退出(并执行渲染帧、从用户处获取输入等操作)。您没有给游戏时间做任何其他事情。
    您几乎肯定希望这是一个协同程序或
    Update
    函数
  • 返回一个介于
    min
    (包含)到
    max
    (独占)之间的整数,因此对它的调用将永远不会返回
    3

  • 以下是一些问题:

    • 水平输入
      垂直输入
      从未在函数中使用

    • while(currentLocation!=finalLocation)
      在某些情况下,此条件可能会失败。Unity使用float作为坐标,这意味着它必须完全位于同一位置,每个小数位必须相同

    • Random。范围(0,3)
      将返回一个介于0(包含)和3(排除)之间的随机数,因此唯一可能的值将是0、1和2。脚本永远不会调用
      Right()
      函数


    默认情况下,Unity使用一个线程运行脚本,如果您放置一个while循环将对象移动到某个位置,它将冻结整个游戏,直到对象位于正确的位置。我建议您使用
    Update()
    函数,它每帧都会被调用。

    以下是一些问题:

    • 水平输入
      垂直输入
      从未在函数中使用

    • while(currentLocation!=finalLocation)
      在某些情况下,此条件可能会失败。Unity使用float作为坐标,这意味着它必须完全位于同一位置,每个小数位必须相同

    • Random。范围(0,3)
      将返回一个介于0(包含)和3(排除)之间的随机数,因此唯一可能的值将是0、1和2。脚本永远不会调用
      Right()
      函数



    默认情况下,Unity使用一个线程运行脚本,如果您放置一个while循环将对象移动到某个位置,它将冻结整个游戏,直到对象位于正确的位置。我建议您使用
    Update()
    函数,它每帧都会被调用。

    此时可能会出现问题,并且当前位置可能总是与最终位置不同,因此脚本也会陷入无休止的循环中,为什么要查询
    Input
    ?@3Dave有什么用?while中可能有问题,当前位置可能总是与最终位置不同,因此脚本也会陷入无休止的循环,为什么要查询
    Input
    ?@3Dave有什么用?
    Random.Range
    使用
    int
    值作为参数返回一个
    int
    值。第二个参数是独占的,因此
    随机。范围(0,3)
    可以返回任何值
    0,1,2
    。。所以它肯定不会返回
    3
    你说得对,我之前只看到了第一个带有float参数的。您好@Draco18s感谢您的输入。我采纳了你的建议并实施了更改,但是更新功能仍然崩溃。@xalalau你是怎么做到的?根本不应该有
    while
    循环@德胡戈我成功了,谢谢。参见上面的代码。有一个问题也许有人可以指导我正确的方向,那就是我希望“用户”能够在网格内移动,而不是像现在这样离开网格。
    Random.Range
    with
    int
    值作为参数返回
    int
    值。第二个参数是独占的,因此
    随机。范围(0,3)
    可以返回任何值
    0,1,2
    。。所以它肯定不会返回
    3
    你说得对,我之前只看到了第一个带有float参数的。您好@Draco18s感谢您的输入。我采纳了你的建议并实施了更改,但是更新功能仍然崩溃。@xalalau你是怎么做到的?根本不应该有
    while
    循环@德胡戈我成功了,谢谢。参见上面的代码。有一个问题可能有人会引导我朝正确的方向前进,那就是我希望“用户”能够在网格内移动,而不是像现在这样离开网格。
    每个小数位都必须相同。
    这并不完全正确:使用
    0.00001
    的精度表示相等。当以固定的步幅移动对象时,至少不是完全不可能到达此处的某个位置(当然你是对的,通常代码没有太多意义),你可能也不想在
    Update
    中调用它。。将每个帧移动到s中的新位置