Commit 8cfc3dea authored by 15박보승's avatar 15박보승 Committed by 18류지석

Greedy Pathfinding 구현

parent 07116f74
This diff is collapsed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BS {
public class NodalPathfinding2DAgent : MonoBehaviour
{
public NodalPathfinding2D pathFinder;
public Vector3 destination;
public List<Vector3> path = new List<Vector3>();
float t = 0.5f;
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
if (path.Count > 0)
{
Gizmos.DrawLine(transform.position, path[0]);
for (int i = 0; i < path.Count - 1; i++)
{
Gizmos.DrawLine(path[i] + new Vector3(0,0,1), path[i + 1] + new Vector3(0, 0, 1));
}
Gizmos.color = Color.red;
Gizmos.DrawSphere(destination, 0.5f);
}
}
private void Update()
{
t -= Time.deltaTime;
if (t < 0)
{
MoveTo(new Vector2(Random.Range(pathFinder.bounds.min.x, pathFinder.bounds.max.x), Random.Range(pathFinder.bounds.min.y, pathFinder.bounds.max.y)));
t = 0.5f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
transform.position = new Vector2(Random.Range(pathFinder.bounds.min.x, pathFinder.bounds.max.x), Random.Range(pathFinder.bounds.min.y, pathFinder.bounds.max.y));
}
if (Input.GetMouseButtonDown(1))
{
MoveTo(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
//path = pathFinder.GetPathGreedy(transform.position, destination);
}
public void MoveTo(Vector3 destination)
{
this.destination = destination;
path = pathFinder.GetPathGreedy(transform.position, destination);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: f4a5a47a8846b8940a9edd3801d55993
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -14,10 +14,12 @@ public class NodalPathfinding2DEditor : Editor ...@@ -14,10 +14,12 @@ public class NodalPathfinding2DEditor : Editor
if (GUILayout.Button("Bake")) if (GUILayout.Button("Bake"))
{ {
myTarget.BakeNodes(); myTarget.BakeNodes();
SceneView.RepaintAll();
} }
if (GUILayout.Button("Clear")) if (GUILayout.Button("Clear"))
{ {
myTarget.ClearNodes(); myTarget.ClearNodes();
SceneView.RepaintAll();
} }
} }
} }
...@@ -221,7 +221,7 @@ Transform: ...@@ -221,7 +221,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 38041422} m_GameObject: {fileID: 38041422}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.76, y: -4.97, z: -41.328003} m_LocalPosition: {x: -4.76, y: -4.97, z: 0}
m_LocalScale: {x: 10, y: 5, z: 0} m_LocalScale: {x: 10, y: 5, z: 0}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -259,7 +259,7 @@ MonoBehaviour: ...@@ -259,7 +259,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: 0.907 pointInterval: 0.324
blockMask: blockMask:
serializedVersion: 2 serializedVersion: 2
m_Bits: 256 m_Bits: 256
...@@ -379,7 +379,7 @@ Transform: ...@@ -379,7 +379,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 614196138} m_GameObject: {fileID: 614196138}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.74, y: -1.72, z: -41.328003} m_LocalPosition: {x: 3.74, y: -1.72, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931} m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -486,7 +486,7 @@ Transform: ...@@ -486,7 +486,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1002756477} m_GameObject: {fileID: 1002756477}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.032, y: 4.9, z: -41.328003} m_LocalPosition: {x: -0.032, y: 4.9, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931} m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -593,7 +593,7 @@ Transform: ...@@ -593,7 +593,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1036674795} m_GameObject: {fileID: 1036674795}
m_LocalRotation: {x: 0, y: 0, z: 0.2588191, w: 0.9659258} m_LocalRotation: {x: 0, y: 0, z: 0.2588191, w: 0.9659258}
m_LocalPosition: {x: -5.48, y: -0.04, z: -41.328003} m_LocalPosition: {x: -5.48, y: -0.04, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931} m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
...@@ -700,12 +700,135 @@ Transform: ...@@ -700,12 +700,135 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1324657432} m_GameObject: {fileID: 1324657432}
m_LocalRotation: {x: 0, y: 0, z: 0.7071068, w: 0.7071068} m_LocalRotation: {x: 0, y: 0, z: 0.7071068, w: 0.7071068}
m_LocalPosition: {x: 3.33, y: -6.5, z: -41.328003} m_LocalPosition: {x: 3.33, y: -6.5, z: 0}
m_LocalScale: {x: 50, y: 5, z: 0} m_LocalScale: {x: 50, y: 5, z: 0}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 6 m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90}
--- !u!1 &1592952600
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1592952604}
- component: {fileID: 1592952603}
- component: {fileID: 1592952602}
- component: {fileID: 1592952601}
m_Layer: 8
m_Name: New Sprite (5)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1592952601
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4a5a47a8846b8940a9edd3801d55993, type: 3}
m_Name:
m_EditorClassIdentifier:
pathFinder: {fileID: 178212234}
destination: {x: 0, y: 0, z: 0}
path: []
--- !u!61 &1592952602
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0.049999997, y: 0.049999997, z: 0.049999997, w: 0.049999997}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 0.16, y: 0.16}
newSize: {x: 0.16, y: 0.16}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.16, y: 0.16}
m_EdgeRadius: 0
--- !u!212 &1592952603
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 0.16, y: 0.16}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &1592952604
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.76, y: -2.64, z: 0}
m_LocalScale: {x: 2, y: 2, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1627406709 --- !u!1 &1627406709
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 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