当我移动角色时,Unity编辑器会冻结(无关紧要),但并非总是如此,我仍然无法确定导致冻结的确切触发器,但是单击鼠标后总是会发生在图块上移动角色。 这是代码的相关部分:
public class TacticsMove : MonoBehaviour {
public LayerMask tileLayer;
private bool moving = false;
private bool jumping = false;
private bool jumpingUp = false;
private bool movingEdge = false;
private bool fallingDown = false;
public float moveSpeed = 2;
public float jumpVelocity = 4.5f;
private Vector3 velocity = new Vector3();
private Vector3 heading = new Vector3();
private Vector3 jumpTarget;
private Unit unit;
private Queue<Tile> path;
private List<Tile> walkableTiles;
void Start(){
unit = GetComponent<Unit>();
}
void Update(){
if(moving){
Walk();
} else if(this.gameObject == TurnManager.GetActiveUnit()) {
CheckMouse();
}
}
private void CheckMouse(){
if(Input.GetMouseButtonUp(0)){
Debug.Log("Check Mouse");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.Log("1");
if(Physics.Raycast(ray, out hit, Mathf.Infinity, tileLayer)){
Debug.Log("2");
if(hit.collider.tag == "Tile") {
Debug.Log("3");
Tile t = hit.collider.GetComponent<Tile>();
if(t.moveable){
Debug.Log("4");
MoveTo(t);
}
}
} else {
Debug.Log("8");
}
}
Debug.Log("5");
if(Input.GetButtonDown("Cancel")){
Debug.Log("6");
MovingCleanUp();
}
Debug.Log("7");
}
public void PrepareMovement(){
Debug.Log("PrepareMovement");
unit.GetCurrentTile().current = true;
int range = (int)(unit.GetCurrentAP() / unit.apToMove.Value);
walkableTiles = unit.GetCurrentTile().TilesInRange(unit.jumpHeight.Value, false, range);
foreach(Tile tile in walkableTiles){
tile.moveable = true;
}
}
public void MoveTo(Tile target){
Debug.Log("MoveTo");
SetStateToMoving(true);
unit.GetCurrentTile().current = false;
path = CreatePath(target);
int apCost = (int)(path.Count * unit.apToMove.Value);
unit.ReduceAP(apCost);
}
private void Walk(){
Debug.Log("Walk");
if (path.Count>0){
Tile t = path.Peek();
Vector3 target = t.transform.position;
// target.y += (halfHeight + t.GetComponent<Collider>().bounds.extents.y)* transform.localScale.y;
target.y += t.GetComponent<Collider>().bounds.extents.y;
if(Vector3.Distance(transform.position, target) >= 0.05f) {
// jumping = transform.position.y != target.y; //correto
SetStateToJumping(transform.position.y != target.y);
if(jumping){
Jump(target);
} else{
CalculateHeading(target);
SetHorizontalVelocity();
}
transform.forward = heading;
transform.position += velocity * Time.deltaTime;
} else{
//Tile center reached
transform.position = target;
path.Dequeue();
}
} else {
MovingCleanUp();
}
}
private Queue<Tile> CreatePath(Tile target){
Debug.Log("CreatePath");
Queue<Tile> path = new Queue<Tile>();
Tile closest = unit.GetCurrentTile();
while(closest != target){
foreach(Tile t in closest.FindNeighbors(unit.jumpHeight.Value, false)){
if(Vector3.Distance(t.transform.position, target.transform.position) < Vector3.Distance(closest.transform.position, target.transform.position)){
closest = t;
}
}
path.Enqueue(closest);
}
return path;
}
}
以下是整个脚本:https://pastebin.com/CD0xR120
我不知道如何调试它,因为没有错误消息,它只是冻结,如以下GIF所示:https://imgur.com/a/TGhy9CS
这是代码中发生的事情:
一切正常,这是我为该游戏编写的第一个脚本,然后一切正常,我在进行其他工作,一切进展顺利,直到无处不在突然冻结。
以下是我尝试做的并没有解决问题的一些事情:
我不知道该怎么办,即使能以某种方式获得有关此问题的更多信息(也许是我不知道的日志文件),也非常感谢任何帮助。我在Unity论坛或google上在这里找到的所有内容似乎都指向一个无限循环,我认为这不是我的情况...
感谢您的帮助和关注!
更新:使用每种方法中的调试消息更新的代码,这是冻结时控制台的输出:
[11:38:53] 5 (78)
[11:38:53] 7 (78)
[11:38:52] PrepareMovement (1)
[11:38:52] Check Mouse (1)
[11:38:52] 1 (1)
[11:38:52] 8 (1)
(我会截取屏幕截图,但是如果窗口没有响应,您将无法执行...) 因此,从该控制台输出中,我只能得出以下结论:
if(Physics.Raycast(ray, out hit, Mathf.Infinity, tileLayer)){
但我不知道射线广播如何冻结统一...