Commit 7d136bc8 authored by 15박보승's avatar 15박보승

Debug on WorldToIndex (Div by zero). SetPath implemented.

parent 9eb0b10d
...@@ -261,7 +261,7 @@ MonoBehaviour: ...@@ -261,7 +261,7 @@ MonoBehaviour:
bounds: bounds:
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 10, y: 10, z: 0} m_Extent: {x: 10, y: 10, z: 0}
pointInterval: 1 pointInterval: 0.314
agentRadius: 0.1 agentRadius: 0.1
blockMask: blockMask:
serializedVersion: 2 serializedVersion: 2
...@@ -871,6 +871,11 @@ MonoBehaviour: ...@@ -871,6 +871,11 @@ MonoBehaviour:
_health: 0 _health: 0
eyesightRange: 5 eyesightRange: 5
eyesightDegree: 89 eyesightDegree: 89
roamingPath:
- {x: 8.72, y: 5.15, z: 0}
- {x: 7.2, y: 8.25, z: 0}
- {x: 1.8, y: 7.3, z: 0}
- {x: 4.8, y: 6.78, z: 0}
blockEyesightMask: blockEyesightMask:
serializedVersion: 2 serializedVersion: 2
m_Bits: 256 m_Bits: 256
......
...@@ -11,9 +11,28 @@ public class Enemy : Actor ...@@ -11,9 +11,28 @@ public class Enemy : Actor
[Range(0, 360)] [Range(0, 360)]
public int eyesightDegree = 60; public int eyesightDegree = 60;
[SerializeField]
private List<Vector3> roamingPath;
[SerializeField] [SerializeField]
private LayerMask blockEyesightMask; private LayerMask blockEyesightMask;
#if UNITY_EDITOR
private void OnDrawGizmos()
{
if (roamingPath.Count < 1)
return;
/*
Gizmos.color = Color.white;
Gizmos.DrawLine(transform.position, roamingPath[0]);
for (int i = 0; i < roamingPath.Count; i++)
{
Gizmos.DrawLine(roamingPath[i], roamingPath[(i + 1) % roamingPath.Count]);
}
*/
}
#endif
protected override void Start() protected override void Start()
{ {
base.Start(); base.Start();
...@@ -21,11 +40,14 @@ public class Enemy : Actor ...@@ -21,11 +40,14 @@ public class Enemy : Actor
mf = GetComponentInChildren<MeshFilter>(); mf = GetComponentInChildren<MeshFilter>();
eyesightMesh = new Mesh(); eyesightMesh = new Mesh();
mf.mesh = eyesightMesh; mf.mesh = eyesightMesh;
agent.SetPath(roamingPath);
} }
private void Update() private void Update()
{ {
UpdateEyesightMesh(); UpdateEyesightMesh();
if (agent.path.Count < 1)
agent.SetPath(roamingPath);
} }
private void UpdateEyesightMesh() private void UpdateEyesightMesh()
...@@ -34,6 +56,7 @@ public class Enemy : Actor ...@@ -34,6 +56,7 @@ public class Enemy : Actor
List<int> indices = new List<int>(); List<int> indices = new List<int>();
vertices.Add(Vector3.zero); vertices.Add(Vector3.zero);
/*
for (int i = -eyesightDegree / 2; i <= eyesightDegree / 2; i++) for (int i = -eyesightDegree / 2; i <= eyesightDegree / 2; i++)
{ {
RaycastHit2D hit = Physics2D.Raycast(transform.position, Quaternion.Euler(0, 0, i) * transform.up, eyesightRange, blockEyesightMask); RaycastHit2D hit = Physics2D.Raycast(transform.position, Quaternion.Euler(0, 0, i) * transform.up, eyesightRange, blockEyesightMask);
...@@ -46,6 +69,20 @@ public class Enemy : Actor ...@@ -46,6 +69,20 @@ public class Enemy : Actor
vertices.Add(quat * (hit.point - new Vector2(transform.position.x, transform.position.y))); vertices.Add(quat * (hit.point - new Vector2(transform.position.x, transform.position.y)));
} }
} }
*/
for (int i = -eyesightDegree / 2; i <= eyesightDegree / 2; i++)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Quaternion.Euler(0, 0, i) * transform.up, eyesightRange, blockEyesightMask);
if (hit.collider == null)
vertices.Add((Quaternion.Euler(0, 0, i) * Vector3.up) * eyesightRange);
else
{
Quaternion quat = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, -transform.rotation.eulerAngles.z);
vertices.Add(quat * (hit.point - new Vector2(transform.position.x, transform.position.y)));
}
}
for (int i = 0; i < vertices.Count - 2; i++) for (int i = 0; i < vertices.Count - 2; i++)
{ {
......
...@@ -107,7 +107,7 @@ namespace BS ...@@ -107,7 +107,7 @@ namespace BS
} }
#endif #endif
private void Start() private void Awake()
{ {
if (!isBaked) if (!isBaked)
BakeNodes(); BakeNodes();
...@@ -547,7 +547,7 @@ namespace BS ...@@ -547,7 +547,7 @@ namespace BS
foreach (var adj in adjs) foreach (var adj in adjs)
{ {
float score = Vector2.Dot((IndexToWorld(adj) - position).normalized, direction.normalized) / (IndexToWorld(adj) - position).magnitude; float score = Vector2.Dot((IndexToWorld(adj) - position).normalized, direction.normalized) / ((IndexToWorld(adj) - position).magnitude + 1);
if (max < score) if (max < score)
{ {
index = adj; index = adj;
......
...@@ -83,6 +83,18 @@ namespace BS { ...@@ -83,6 +83,18 @@ namespace BS {
} }
} }
public void SetPath(List<Vector3> path)
{
this.destination = path[path.Count - 1];
List<Vector3> newPath = new List<Vector3>();
newPath.Add(transform.position);
foreach (var next in path)
{
newPath.AddRange(pathFinder.GetPathAstar(newPath[newPath.Count - 1], next));
}
this.path = newPath;
}
public void Move(Vector2 direction) public void Move(Vector2 direction)
{ {
...@@ -101,7 +113,7 @@ namespace BS { ...@@ -101,7 +113,7 @@ namespace BS {
{ {
path.RemoveAt(0); path.RemoveAt(0);
} }
else if (path.Count > 1 && Vector2.Distance(transform.position, path[0]) < pathFinder.pointInterval / 2) else if (path.Count > 1 && Vector2.Distance(transform.position, path[0]) < pathFinder.pointInterval / 10)
{ {
path.RemoveAt(0); path.RemoveAt(0);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment