Enemy AI with flying - when it's working

This commit is contained in:
Norbi Peti 2020-12-08 02:51:32 +01:00
parent 65542aea59
commit 78d1d5419c
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 174 additions and 5 deletions

View file

@ -58,6 +58,7 @@
<Compile Include="Assets\Scripts\AchievementMenuManager.cs" />
<Compile Include="Assets\Scripts\CameraController.cs" />
<Compile Include="Assets\Scripts\CharacterControllerBase.cs" />
<Compile Include="Assets\Scripts\EnemyAI.cs" />
<Compile Include="Assets\Scripts\EnemyController.cs" />
<Compile Include="Assets\Scripts\EnemySpawner.cs" />
<Compile Include="Assets\Scripts\HUDManager.cs" />

View file

@ -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

View file

@ -6967,7 +6967,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
version: 1
data:
dataString: UEsDBBQAAAgIAAAIIex9l9BFcQIAABgFAAALACQAZ3JhcGgwLmpzb24KACAAAAAAAAEAGAAAgD7V3rGdAQCAPtXesZ0BAIA+1d6xnQFlVMty4jAQ/BVK54WyDTHO3nYdQvaQR4Vs5eKLkMdYhZBcegRIin/fkWyww54Q0zOj7p6RvwiXpgFmlV5qXj6qEsjPEfFn8mPUgw9woBslV/wTzjnvvLS1T6Ih55VarjAe+zKjdmA1Z7/kRvjkCINO8krp3aLcQK6MNRiuqDCAkFbWV0sMfY3IAX/Gt77kiKdk7k+feLqNRic8MpAWdJ+aJJPkJkqyZNpVpJMkS9Np0pXNJlGWZfE8XYyjeejgJBN010Dp5fSN4uh8Z9zeJFFpl+JVMSUENx1NYo9NsCGnjXEoEhNKTlF24Obza+Cb2nqGw+LnqjJgO080Pd5xje61Xclv1Vq6o2bbEvugwgUHA6O25eN/6DgOcKXV7uF8ayvH1pxtX+mRUWMHjg/Dd99pO7mnYkvXAt5rkE9qqZWTJYJWO1/qDCR3/d+LsLwGtu3jLdWroGt6uy8DnkyzWZyF8XQj83JOwYZDLvhu7bMnszawEioYHxYENI7oD3IP62O6rhj1hP4aeKOb4Z51wD3XxiLUCZae6Vo57VPJItjnhTmbKy0hhDv+DUgq7PEF29jrWV5hg2uvkHvqX1R3eYedH8pVSQj3+VF0Bb2o/WVuplb7RzD1s7OCS+hJe+AJfcqVlO2qDT05l62crigblG0cL79vGUlu5/E6Ttclm84ims7Gs4ylEb2pbuI1S2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GjVru5LfqhWypmbXYvugwvk5ySwNsNqmj//Fx3EIl1rVD+e5LSdb8WL3So8FNXag9NC9+A7cyT0VO7oR8F6BfFIrrZxkGLTa+VJnIFn0fy/UsgqKXe9voV45XdNrflnszWQ+jefpJF2Oo9tuW57QKUhxyASvN2EP09axFiqIH04DNG7qD6IPh2O6vuj1kP4aeKPb4YV1gXuujcVQR1l6rBvltE8lyyCgp+ZsprSE4O4YNCCpsMcXbGOv93kVG4y9itxT/5a64V3s/ESuSoK7z4+iq9CL2l82Zyq1fwRTPTsruIQetA88oU6ZkrI9t6Em57K10yUtBmVbx9n3OyPJ3W28idMNKybTiKbT8XRepBGdlbN4U6QxnZBwiFwiTypeWqCdPLg12TeXeHTnT8xopWkTTp9pul/xz1oNJOeyVOtCA8jntsMFOGgcgq+ULRlHgdZgLZdh3+QrJ0IVO2D5pVEemC4PVtO875KThn8o6z05ycI3JScncvoHUEsDBBQAAAgIAAAIIeyM5Es0bQAAAHgAAAAJACQAbWV0YS5qc29uCgAgAAAAAAABABgAAIA+1d6xnQEAgD7V3rGdAQCAPtXesZ0Bq1YqSy0qzszPU7JSMNEz0jM01VFQSi9KLMgoBooYgjilmSkgdrSSkaW5YZKhWVJKsrGJQaKZia6JRbKZQaJpmqlhUrKZYaKxUixQfUllQapfYm4qRE9AYklGWmZeSmZeup57UWaKO8hopdhaAFBLAQItABQAAAgIAAAIIeyaplw6awIAABIFAAALACQAAAAAAAAAAAAAAAAAAABncmFwaDAuanNvbgoAIAAAAAAAAQAYAACAPtXesZ0BAIA+1d6xnQEAgD7V3rGdAVBLAQItABQAAAgIAAAIIeyM5Es0bQAAAHgAAAAJACQAAAAAAAAAAAAAALgCAABtZXRhLmpzb24KACAAAAAAAAEAGAAAgD7V3rGdAQCAPtXesZ0BAIA+1d6xnQFQSwUGAAAAAAIAAgC4AAAAcAMAAAAA
upgradeData:
file_cachedStartup: {fileID: 0}
data_cachedStartup:

118
Assets/Scripts/EnemyAI.cs Normal file
View file

@ -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<Seeker>();
_rb = GetComponent<Rigidbody2D>();
_controller = GetComponent<EnemyController>();
_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++;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d913646642414964b085fc021ad0fb00
timeCreated: 1607388688

View file

@ -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;