플레이어에는
public class Player_S : MonoBehaviour
{
// Start is called before the first frame update
public List<GameObject> OpenObj = new List<GameObject>();
public List<GameObject> CloseObj = new List<GameObject>();
public List<GameObject> FinishObj = new List<GameObject>();
public GameObject SelectedObj;
public GameObject GoalObj;
public GameObject UnderTile;
public bool PathFinding;
void Start()
{
}
// Update is called once per frame
void Update()
{
Click();
}
void Click()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D raycast = Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition).origin, Camera.main.ScreenPointToRay(Input.mousePosition).direction);
if(raycast.collider != null)
{
switch (raycast.collider.tag)
{
case "Floor":
Debug.Log(raycast.collider.name);
PathFind(UnderTile,raycast.collider.gameObject);
break;
}
}
}
}
void PathFind(GameObject StartObj , GameObject GoalObj_)
{
PathFinding = true;
GoalObj = GoalObj_;
SelectedObj = StartObj;
OpenObj.Add(SelectedObj);
Find();
Finish();
}
void Find()
{
Select();
Scan();
if (SelectedObj == GoalObj)
PathFinding = false;
if(PathFinding)
Find();
}
void Scan()
{
Collider2D[] raycast = Physics2D.OverlapBoxAll(SelectedObj.transform.position,new Vector2(2,2),0,LayerMask.GetMask("Floor"));
foreach(Collider2D col in raycast)
{
Floor_S col_S = col.GetComponent<Floor_S>();
Floor_S Sel_S = SelectedObj.GetComponent<Floor_S>();
if (!(col_S.IsWall || CloseObj.Contains(col.gameObject)))
{
OpenObj.Add(col.gameObject);
col_S.LastDis = (GoalObj.transform.position - col.transform.position).magnitude;
if (Sel_S.MoveDis + (SelectedObj.transform.position - col.transform.position).magnitude < col_S.MoveDis || col_S.BeforeObj == null)
{
col_S.MoveDis = Sel_S.MoveDis + (SelectedObj.transform.position - col.transform.position).magnitude;
col_S.BeforeObj = SelectedObj;
}
col_S.Value = col_S.MoveDis + col_S.LastDis;
}
}
}
GameObject Select()
{
for (int i = 0; i < OpenObj.Count; i++)
{
Floor_S Open_S = OpenObj[i].GetComponent<Floor_S>();
Floor_S Selected_S = SelectedObj.GetComponent<Floor_S>();
if (Open_S.Value < Selected_S.Value || (Open_S.Value == Selected_S.Value && Open_S.MoveDis > Selected_S.MoveDis))
SelectedObj = OpenObj[i];
}
OpenObj.Remove(SelectedObj);
CloseObj.Add(SelectedObj);
return SelectedObj;
}
void Finish()
{
}
private void OnTriggerStay2D(Collider2D collision)
{
UnderTile = collision.gameObject;
}
}
이거하고 타일에는
public class Floor_S : MonoBehaviour
{
// Start is called before the first frame update
public GameObject BeforeObj;
public float Value;
public float MoveDis;
public float LastDis;
public bool IsWall;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
IsWall = true;
}
private void OnTriggerExit2D(Collider2D collision)
{
IsWall = false;
}
}
넣었는데 유니티가 갑자기 멈춰버려
밖이라 자세히는 못보는데, 유니티가 멈춘다면 어딘가에서 무한루프 돌고 있을 가능성이 있어. 루프문에서 컨디션 안 맞는 곳은 없니?