From 78d1d5419ca349916b0119e73d855efe7280f8d7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 8 Dec 2020 02:51:32 +0100 Subject: [PATCH] Enemy AI with flying - when it's working --- Assembly-CSharp.csproj | 1 + Assets/Prefabs/Enemy.prefab | 47 ++++++++++++ Assets/Scenes/Main.unity | 2 +- Assets/Scripts/EnemyAI.cs | 118 ++++++++++++++++++++++++++++++ Assets/Scripts/EnemyAI.cs.meta | 3 + Assets/Scripts/EnemyController.cs | 8 +- 6 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 Assets/Scripts/EnemyAI.cs create mode 100644 Assets/Scripts/EnemyAI.cs.meta diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index 1e1c242..20eb4f4 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -58,6 +58,7 @@ + diff --git a/Assets/Prefabs/Enemy.prefab b/Assets/Prefabs/Enemy.prefab index 339ab33..528fe0b 100644 --- a/Assets/Prefabs/Enemy.prefab +++ b/Assets/Prefabs/Enemy.prefab @@ -43,6 +43,8 @@ GameObject: - component: {fileID: 7250562544566202696} - component: {fileID: 7250562544566202697} - component: {fileID: -1018939148389277147} + - component: {fileID: 9186701644465693241} + - component: {fileID: -5685260035900252778} m_Layer: 9 m_Name: Enemy m_TagString: Enemy @@ -183,3 +185,48 @@ MonoBehaviour: speed: 8 flyForce: 30 finalHealth: 3 +--- !u!114 &9186701644465693241 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7250562544566202694} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 373b52eb9bf8c40f785bb6947a1aee66, type: 3} + m_Name: + m_EditorClassIdentifier: + version: 1 + drawGizmos: 1 + detailedGizmos: 0 + startEndModifier: + addPoints: 0 + exactStartPoint: 3 + exactEndPoint: 3 + useRaycasting: 0 + mask: + serializedVersion: 2 + m_Bits: 4294967295 + useGraphRaycasting: 0 + traversableTags: -1 + tagPenalties: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + graphMask: + value: -1 +--- !u!114 &-5685260035900252778 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7250562544566202694} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d913646642414964b085fc021ad0fb00, type: 3} + m_Name: + m_EditorClassIdentifier: + updateRate: 4 + speed: 500 + fMode: 0 + pathIsEnded: 0 + nextWaypointDistance: 4 diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 1804a61..7ef98fc 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -6967,7 +6967,7 @@ MonoBehaviour: m_EditorClassIdentifier: version: 1 data: - dataString: UEsDBBQAAAgIAAAIIex9l9BFcQIAABgFAAALACQAZ3JhcGgwLmpzb24KACAAAAAAAAEAGAAAgD7V3rGdAQCAPtXesZ0BAIA+1d6xnQFlVMty4jAQ/BVK54WyDTHO3nYdQvaQR4Vs5eKLkMdYhZBcegRIin/fkWyww54Q0zOj7p6RvwiXpgFmlV5qXj6qEsjPEfFn8mPUgw9woBslV/wTzjnvvLS1T6Ih55VarjAe+zKjdmA1Z7/kRvjkCINO8krp3aLcQK6MNRiuqDCAkFbWV0sMfY3IAX/Gt77kiKdk7k+feLqNRic8MpAWdJ+aJJPkJkqyZNpVpJMkS9Np0pXNJlGWZfE8XYyjeejgJBN010Dp5fSN4uh8Z9zeJFFpl+JVMSUENx1NYo9NsCGnjXEoEhNKTlF24Obza+Cb2nqGw+LnqjJgO080Pd5xje61Xclv1Vq6o2bbEvugwgUH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QAAAgIAAAIIeyaplw6awIAABIFAAALACQAAAAAAAAAAAAAAAAAAABncmFwaDAuanNvbgoAIAAAAAAAAQAYAACAPtXesZ0BAIA+1d6xnQEAgD7V3rGdAVBLAQItABQAAAgIAAAIIeyM5Es0bQAAAHgAAAAJACQAAAAAAAAAAAAAALgCAABtZXRhLmpzb24KACAAAAAAAAEAGAAAgD7V3rGdAQCAPtXesZ0BAIA+1d6xnQFQSwUGAAAAAAIAAgC4AAAAcAMAAAAA upgradeData: file_cachedStartup: {fileID: 0} data_cachedStartup: diff --git a/Assets/Scripts/EnemyAI.cs b/Assets/Scripts/EnemyAI.cs new file mode 100644 index 0000000..f53c731 --- /dev/null +++ b/Assets/Scripts/EnemyAI.cs @@ -0,0 +1,118 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Pathfinding; + +[RequireComponent(typeof(Rigidbody2D))] +[RequireComponent(typeof(Seeker))] +public class EnemyAI : MonoBehaviour +{ + // What to chase? + private Transform _target; + + // How many times each second we will update our path + public float updateRate = 2f; + + // Caching + private Seeker _seeker; + private Rigidbody2D _rb; + + //The calculated path + private Path _path; + + //The AI's speed per second + public float speed = 30f; + public ForceMode2D fMode; + + [HideInInspector] public bool pathIsEnded; + + // The max distance from the AI to a waypoint for it to continue to the next waypoint + public float nextWaypointDistance = 2; + + // The waypoint we are currently moving towards + private int _currentWaypoint; + + private EnemyController _controller; + + // Start is called before the first frame update + void Start() + { + _seeker = GetComponent(); + _rb = GetComponent(); + _controller = GetComponent(); + _target = _controller.target; + + if (_target == null) + { + Debug.LogError("No Player found? PANIC!"); + return; + } + + // Start a new path to the target position, return the result to the OnPathComplete method + _seeker.StartPath(transform.position, _target.position, OnPathComplete); + + StartCoroutine(UpdatePath()); + } + + IEnumerator UpdatePath() + { + // Start a new path to the target position, return the result to the OnPathComplete method + _seeker.StartPath(transform.position, _target.position, OnPathComplete); + + yield return new WaitForSeconds(1f / updateRate); + if (_controller.IsAlive()) + StartCoroutine(UpdatePath()); + } + + public void OnPathComplete(Path p) + { + Debug.Log("We got a path. Did it have an error? " + p.error); + + if (!p.error) + { + _path = p; + _currentWaypoint = 0; + } + } + + void FixedUpdate() + { + if (_path == null) + return; + + if (!_controller.IsAlive()) + return; + + if (_currentWaypoint >= _path.vectorPath.Count) + { + if (pathIsEnded) + return; + + Debug.Log("End of path reached."); + pathIsEnded = true; + return; + } + + pathIsEnded = false; + + //Direction to the next waypoint + Vector3 dir = (_path.vectorPath[_currentWaypoint] - transform.position).normalized; + dir *= speed * Time.fixedDeltaTime; + + //if(Vector3.Distance (transform.position, _target.position) < 10) + //{ + //Move the AI + //_rb.AddForce(dir, fMode); + _rb.velocity = dir; + //} + + if (_path.vectorPath[_currentWaypoint].y - transform.position.y > 1) + _rb.AddForce(new Vector2(0, 10f)); + + float dist = Vector3.Distance(transform.position, _path.vectorPath[_currentWaypoint]); + if (dist < nextWaypointDistance) + { + _currentWaypoint++; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/EnemyAI.cs.meta b/Assets/Scripts/EnemyAI.cs.meta new file mode 100644 index 0000000..ac04464 --- /dev/null +++ b/Assets/Scripts/EnemyAI.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d913646642414964b085fc021ad0fb00 +timeCreated: 1607388688 \ No newline at end of file diff --git a/Assets/Scripts/EnemyController.cs b/Assets/Scripts/EnemyController.cs index c66d969..2765847 100644 --- a/Assets/Scripts/EnemyController.cs +++ b/Assets/Scripts/EnemyController.cs @@ -49,11 +49,11 @@ public class EnemyController : CharacterControllerBase if (diff.y > 5 || diff.x > 20) Remove(); - if (diff.y > 1) - _rb.AddForce(new Vector2(0, 10f)); + /*if (diff.y > 1) + _rb.AddForce(new Vector2(0, 10f));*/ diff.Normalize(); - float sp = ((float) _random.NextDouble() / 2f + 1f) * speed; //1 és 1.5 közötti szorzó - _rb.AddForce(diff * sp); + /*float sp = ((float) _random.NextDouble() / 2f + 1f) * speed; //1 és 1.5 közötti szorzó + _rb.AddForce(diff * sp);*/ if (diff.x * transform.localScale.x < 0) //Ha másfelé néz, mint amerre megy { var scale = tr.localScale;