From de82e91d9fc9559f794ed4b7d75255c87cc5bf45 Mon Sep 17 00:00:00 2001 From: KrolMel Date: Mon, 2 Jan 2023 19:31:45 +0100 Subject: [PATCH] Wizard Astar fix --- Assets/Scenes/SampleScene.unity | 18 ++++++++++++----- Assets/Scenes/WizardHouse.unity | 4 ++-- .../FollowingPatrollingEnemy.cs | 18 ++++++++++++----- Assets/Scripts/NPCs' Scripts/NPCFollowing.cs | 14 +++++++++++-- .../Pathfinding/AStarPathfindingAgent.cs | 20 ++++++++++++++----- Assets/Scripts/Pathfinding/Pathfinding.cs | 7 ++++--- 6 files changed, 59 insertions(+), 22 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 45ea4e76..c4d09e94 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -490,6 +490,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!58 &8620313 CircleCollider2D: @@ -1165,6 +1166,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!114 &68076409 MonoBehaviour: @@ -1556,6 +1558,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!50 &112145426 Rigidbody2D: @@ -2782,7 +2785,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 242009510} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -21.69, y: 21.69, z: 10} + m_LocalPosition: {x: -18.75, y: 21.69, z: 10} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 184660453} @@ -2834,7 +2837,7 @@ MonoBehaviour: chaseRadius: 6 attackRadius: 0.55 homePosition: {fileID: 1474164377} - roundingDistance: 0 + roundingDistance: 0.06 anim: {fileID: 0} other: {fileID: 0} inRange: 0 @@ -2912,6 +2915,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!58 &242009518 CircleCollider2D: @@ -21426,6 +21430,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!114 &340745814 MonoBehaviour: @@ -28021,6 +28026,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!50 &641696204 Rigidbody2D: @@ -28395,6 +28401,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!95 &682072032 Animator: @@ -40901,6 +40908,7 @@ MonoBehaviour: worldPosition: {x: 0, y: 0, z: 0} gridPosition: {x: 0, y: 0} isChasing: 0 + point: {fileID: 0} path: [] --- !u!50 &1072778926 Rigidbody2D: @@ -44843,7 +44851,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1474164376} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -21.71, y: 21.61, z: 0} + m_LocalPosition: {x: -14.45, y: 24.48, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 166697418} @@ -150475,7 +150483,7 @@ GameObject: m_TagString: Untagged m_Icon: {fileID: -5442936267250999957, guid: 0000000000000000d000000000000000, type: 0} m_NavMeshLayer: 0 - m_StaticEditorFlags: 4294967295 + m_StaticEditorFlags: 0 m_IsActive: 1 --- !u!4 &2105401425 Transform: @@ -150485,7 +150493,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2105401424} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -21.71, y: 17.02, z: 0} + m_LocalPosition: {x: -23.27, y: 17.59, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 166697418} diff --git a/Assets/Scenes/WizardHouse.unity b/Assets/Scenes/WizardHouse.unity index 7a3773d4..4095b34a 100644 --- a/Assets/Scenes/WizardHouse.unity +++ b/Assets/Scenes/WizardHouse.unity @@ -39520,11 +39520,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 4759712171538235753, guid: 417d111e604a8124cb960805d809e513, type: 3} propertyPath: m_LocalPosition.x - value: -1.9412931 + value: -1.53 objectReference: {fileID: 0} - target: {fileID: 4759712171538235753, guid: 417d111e604a8124cb960805d809e513, type: 3} propertyPath: m_LocalPosition.y - value: -2.068871 + value: -3.19 objectReference: {fileID: 0} - target: {fileID: 4759712171538235753, guid: 417d111e604a8124cb960805d809e513, type: 3} propertyPath: m_LocalPosition.z diff --git a/Assets/Scripts/Enemies' Scprits/FollowingPatrollingEnemy.cs b/Assets/Scripts/Enemies' Scprits/FollowingPatrollingEnemy.cs index 2a676e4c..60d8c3f8 100644 --- a/Assets/Scripts/Enemies' Scprits/FollowingPatrollingEnemy.cs +++ b/Assets/Scripts/Enemies' Scprits/FollowingPatrollingEnemy.cs @@ -15,7 +15,7 @@ public class FollowingPatrollingEnemy : Enemy public float attackRadius; public Transform homePosition; - public float roundingDistance; + public float roundingDistance = 0.2f; private Rigidbody2D myRigidbody; public Animator anim; public GameObject other; @@ -70,6 +70,8 @@ public class FollowingPatrollingEnemy : Enemy anim = GetComponent(); target = GameObject.FindWithTag("Player").transform; other = GameObject.FindWithTag("Player"); + + roundingDistance = 1.3f; } // Update is called once per frame @@ -131,16 +133,20 @@ public class FollowingPatrollingEnemy : Enemy } else if (Vector2.Distance(target.position, transform.position) > chaseRadius) { - + //Debug.Log(Vector2.Distance(transform.position, path[currentPoint].position)); - + if (Vector2.Distance(transform.position, path[currentPoint].position) > roundingDistance) { - StopAllCoroutines(); - transform.position = Vector2.MoveTowards(transform.position, path[currentPoint].position, moveSpeed * Time.deltaTime); + //transform.position = Vector2.MoveTowards(transform.position, path[currentPoint].position, moveSpeed * Time.deltaTime); + agent.point = path[currentPoint].position; + + agent.FindPoint(); + StartCoroutine(agent.FollowPath()); } else { + Debug.Log("Change"); changeGoal(); } } @@ -226,6 +232,7 @@ public class FollowingPatrollingEnemy : Enemy private void changeGoal() { + Debug.Log("changeGoal Goal"); if (currentPoint == path.Length - 1) { currentPoint = 0; @@ -236,5 +243,6 @@ public class FollowingPatrollingEnemy : Enemy currentPoint++; currentGoal = path[currentPoint]; } + Debug.Log(currentPoint); } } diff --git a/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs b/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs index ba51980a..03395275 100644 --- a/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs +++ b/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs @@ -81,17 +81,27 @@ public class NPCFollowing : MonoBehaviour } else if (approaching) { - //start dialogue here we want uga bunga + //start dialogue here gameObject.GetComponent().State = NPCStateEnum.Talking; + + agent.path.Clear(); // if we are able to talgk we dont want go go further player } } else if (!approaching && isAfterAction) { + Debug.Log(Vector2.Distance(transform.position, homePosition)); + Debug.Log(homePosition); if (Vector2.Distance(transform.position, homePosition) > 0.05) { - transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime); + //transform.position = Vector2.MoveTowards(transform.position, homePosition, 2 * Time.deltaTime); + agent.point = homePosition; + + + agent.FindPoint(); + + StartCoroutine(agent.FollowPath()); } else { diff --git a/Assets/Scripts/Pathfinding/AStarPathfindingAgent.cs b/Assets/Scripts/Pathfinding/AStarPathfindingAgent.cs index 37e43419..0b6daa44 100644 --- a/Assets/Scripts/Pathfinding/AStarPathfindingAgent.cs +++ b/Assets/Scripts/Pathfinding/AStarPathfindingAgent.cs @@ -11,6 +11,8 @@ public class AStarPathfindingAgent : MonoBehaviour public Node currentPosition; public bool isChasing; private Animator myAnim; + + public Vector3 point; public List path; // Start is called before the first frame update @@ -37,8 +39,16 @@ public class AStarPathfindingAgent : MonoBehaviour public void FindPath() { - Pathfinder.FindPath(transform.position,Pathfinder.Player.position, this); - + var player = FindObjectOfType().gameObject; + + point = player.transform.position; + + FindPoint(); + } + + public void FindPoint() + { + Pathfinder.FindPath(transform.position, point, this); } public IEnumerator FollowPath() @@ -60,7 +70,7 @@ public class AStarPathfindingAgent : MonoBehaviour { currentWorldPosition = transform.position; //if (transform.position == currentWaypoint.worldPosition) - if (Vector3.Distance(transform.position, currentWaypoint.worldPosition)<0.01f) + if (Vector3.Distance(transform.position, currentWaypoint.worldPosition)<0.001f) { targetIndex++; bool reachedPosition = targetIndex >= path.Count; @@ -74,11 +84,11 @@ public class AStarPathfindingAgent : MonoBehaviour Vector3 current = transform.position; Vector3 target = currentWaypoint.worldPosition; - if (Pathfinder.Player.position.x < current.x) + if (point.x < current.x) { myAnim.SetBool("Left", true); } - if (Pathfinder.Player.position.x >= current.x) + if (point.x >= current.x) { myAnim.SetBool("Left", false); } diff --git a/Assets/Scripts/Pathfinding/Pathfinding.cs b/Assets/Scripts/Pathfinding/Pathfinding.cs index 5b78b63b..ba98e3b8 100644 --- a/Assets/Scripts/Pathfinding/Pathfinding.cs +++ b/Assets/Scripts/Pathfinding/Pathfinding.cs @@ -4,13 +4,13 @@ using UnityEngine; public class Pathfinding : MonoBehaviour { - public Transform Player; + // public Transform Player; public NodeMap Map; // Start is called before the first frame update void Start() { - Player = FindObjectOfType().transform; + //Player = FindObjectOfType().transform; } // Update is called once per frame @@ -28,7 +28,8 @@ public class Pathfinding : MonoBehaviour openSet.Add(startNode); while (openSet.Count > 0) { - Node node = openSet[0]; + Node node = openSet[0]; + for (int i = 1; i < openSet.Count; i ++) { if (openSet[i].FCost < node.FCost || openSet[i].FCost == node.FCost) { if (openSet[i].hCost < node.hCost)