c# - 随机移动脚本导致 Unity 冻结
问题描述
我有一个由 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;
}
}
}
我的最后一个问题是如何将这种随机性限制为仅坚持网格而不脱离网格。有什么想法吗?
解决方案
几个问题:
- 您的对象陷入无限
while
循环,因为在您的随机移动脚本到达其finalLocation
. 你没有给游戏时间做任何其他事情。
你几乎肯定希望这是一个协程或Update
函数。 Random.Range
返回一个 int 范围min
(包括)到max
(不包括),所以你对它的调用永远不会返回3
。