From 6f61c8a7c440fb7f1c1a4fc581757d8db2211356 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 8 Dec 2020 10:14:32 +0100 Subject: [PATCH] Fix AI --- Assets/Scripts/EnemyAI.cs | 15 +++++++++------ Assets/Scripts/NavMeshUpdater.cs | 2 +- Projekt.sln | 22 +++++++++++----------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/EnemyAI.cs b/Assets/Scripts/EnemyAI.cs index f53c731..b4c6047 100644 --- a/Assets/Scripts/EnemyAI.cs +++ b/Assets/Scripts/EnemyAI.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -51,6 +52,11 @@ public class EnemyAI : MonoBehaviour // Start a new path to the target position, return the result to the OnPathComplete method _seeker.StartPath(transform.position, _target.position, OnPathComplete); + //StartCoroutine(UpdatePath()); + } + + private void OnEnable() + { StartCoroutine(UpdatePath()); } @@ -99,15 +105,12 @@ public class EnemyAI : MonoBehaviour Vector3 dir = (_path.vectorPath[_currentWaypoint] - transform.position).normalized; dir *= speed * Time.fixedDeltaTime; - //if(Vector3.Distance (transform.position, _target.position) < 10) - //{ + float diff = Vector3.Distance(transform.position, _target.position); //Move the AI - //_rb.AddForce(dir, fMode); - _rb.velocity = dir; - //} + _rb.AddForce(dir * 10 / diff, fMode); if (_path.vectorPath[_currentWaypoint].y - transform.position.y > 1) - _rb.AddForce(new Vector2(0, 10f)); + _rb.AddForce(new Vector2(0, 20f)); float dist = Vector3.Distance(transform.position, _path.vectorPath[_currentWaypoint]); if (dist < nextWaypointDistance) diff --git a/Assets/Scripts/NavMeshUpdater.cs b/Assets/Scripts/NavMeshUpdater.cs index 9e8ca32..784ead6 100644 --- a/Assets/Scripts/NavMeshUpdater.cs +++ b/Assets/Scripts/NavMeshUpdater.cs @@ -20,7 +20,7 @@ public class NavMeshUpdater : MonoBehaviour private void Update() { - if (Mathf.Abs(player.position.x - _gg.center.x) > _gg.width / 4f) + if (Mathf.Abs(player.position.x - _gg.center.x) > _gg.width * _gg.nodeSize / 4f) { var pos = _gg.center; pos.x = player.position.x; diff --git a/Projekt.sln b/Projekt.sln index 10c5fc4..865e836 100644 --- a/Projekt.sln +++ b/Projekt.sln @@ -1,34 +1,30 @@  Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AstarPathfindingProject", "AstarPathfindingProject.csproj", "{5CCEE858-6C72-CBFD-AB4D-C14A74568B2C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp-Editor", "Assembly-CSharp-Editor.csproj", "{FDE76E55-8F95-B17A-F5EB-F218F1B07F99}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AstarPathfindingProjectEditor", "AstarPathfindingProjectEditor.csproj", "{D490F06A-6B5E-2FC6-4BC7-5DE43DB4B967}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PackageToolsEditor", "PackageToolsEditor.csproj", "{F32E2685-1CA3-72C0-A278-83F3E677A98E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp-Editor", "Assembly-CSharp-Editor.csproj", "{FDE76E55-8F95-B17A-F5EB-F218F1B07F99}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Release|Any CPU.Build.0 = Release|Any CPU {5CCEE858-6C72-CBFD-AB4D-C14A74568B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5CCEE858-6C72-CBFD-AB4D-C14A74568B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU {5CCEE858-6C72-CBFD-AB4D-C14A74568B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU {5CCEE858-6C72-CBFD-AB4D-C14A74568B2C}.Release|Any CPU.Build.0 = Release|Any CPU - {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Release|Any CPU.Build.0 = Release|Any CPU + {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78B42BEC-C559-63E3-6996-7DE1ABC1B6B0}.Release|Any CPU.Build.0 = Release|Any CPU {D490F06A-6B5E-2FC6-4BC7-5DE43DB4B967}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D490F06A-6B5E-2FC6-4BC7-5DE43DB4B967}.Debug|Any CPU.Build.0 = Debug|Any CPU {D490F06A-6B5E-2FC6-4BC7-5DE43DB4B967}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -37,6 +33,10 @@ Global {F32E2685-1CA3-72C0-A278-83F3E677A98E}.Debug|Any CPU.Build.0 = Debug|Any CPU {F32E2685-1CA3-72C0-A278-83F3E677A98E}.Release|Any CPU.ActiveCfg = Release|Any CPU {F32E2685-1CA3-72C0-A278-83F3E677A98E}.Release|Any CPU.Build.0 = Release|Any CPU + {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FDE76E55-8F95-B17A-F5EB-F218F1B07F99}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE