简体   繁体   English

随机移动脚本导致 Unity 冻结

[英]Random Movement Script causes Unity to freeze

I have a grid consisting of 16 tiles.我有一个由 16 个图块组成的网格。 Now basically I want the user to find a path to the final location by moving randomly on the choices he has within the grid.现在基本上我希望用户通过随机移动他在网格中的选择来找到通往最终位置的路径。

As of now I managed to create the functions for moving a step up, down, left and right.到目前为止,我设法创建了向上、向下、向左和向右移动台阶的功能。 The issue arises when I'm trying to code in random movement.当我尝试以随机运动进行编码时,就会出现问题。 Ideally this is setup in a way that he can't go off bounds from the grid.理想情况下,这是以他不能 go 超出网格边界的方式设置的。

This is what I got going:这就是我要做的:

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

        }
    }
}

UPDATED WORKING CODE:更新的工作代码:

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

My last issue is how I can limit this randomness to only stick to the grid and not go off grid.我的最后一个问题是如何将这种随机性限制为仅坚持网格而不是 go 离网。 Any thoughts?有什么想法吗?

Several problems:几个问题:

  1. Your object is stuck in an endless while loop because it isn't allowed to exit (and do things like render the frame, take input from the user, etc) until your random movement script reaches its finalLocation .您的 object 卡在一个无休止的while循环中,因为在您的随机移动脚本到达其finalLocation之前,它不允许退出(并执行诸如渲染帧、获取用户输入等操作)。 You aren't giving the game time to do anything else.你没有给游戏时间做任何其他事情。
    You almost certainly want this to be a coroutine or an Update function.您几乎肯定希望这是一个协程或Update function。
  2. Random.Range returns an int in the range min (inclusive) to max (exclusive), so your call to it will never return 3 . Random.Range返回min (包括)到max (不包括)范围内的 int,因此您对它的调用将永远不会返回3

Here are some problems:这里有一些问题:

  • horizontalInput and verticalInput are never used inside your functions horizontalInputverticalInput永远不会在你的函数中使用

  • while(currentLocation != finalLocation) This condition can fail in some situations. while(currentLocation != finalLocation)这种情况在某些情况下可能会失败。 Unity uses float for the coordinates, meaning it needs to be exactly on the same position, every decimal place need to be the same. Unity 使用浮点数作为坐标,这意味着它需要在同一个 position 上,每个小数位都必须相同。

  • Random.Range(0, 3) will return a random number between 0 (inclusive) and 3 (exclusive), so the only possible values will be 0, 1 and 2. The script will never call the Right() function. Random.Range(0, 3)将返回一个介于 0(包括)和 3(不包括)之间的随机数,因此唯一可能的值将是 0、1 和 2。脚本永远不会调用Right() function。

Unity uses one thread to run your scripts by default, if you put a while loop to move the object to a location it will freeze the entire game till the object is at the proper place.默认情况下,Unity 使用一个线程来运行您的脚本,如果您放置一个 while 循环将 object 移动到某个位置,它将冻结整个游戏,直到 object 位于正确的位置。 I recommend you to use the Update() function, it gets called every frame.我建议您使用Update() function,它每帧都会被调用。

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

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