diff --git a/Assets/Scenes/Prototype.unity b/Assets/Scenes/Prototype.unity index 88cae0a..64edaa8 100644 --- a/Assets/Scenes/Prototype.unity +++ b/Assets/Scenes/Prototype.unity @@ -435,6 +435,7 @@ - component: {fileID: 743703311} - component: {fileID: 743703309} - component: {fileID: 743703310} + - component: {fileID: 743703313} m_Layer: 0 m_Name: Orc m_TagString: Untagged @@ -528,6 +529,7 @@ stop: 0 agentDestination: {x: 0, y: 0} moveDirection: {x: 0, y: 0} + rDist: 0 --- !u!114 &743703310 MonoBehaviour: m_ObjectHideFlags: 0 @@ -590,6 +592,28 @@ m_HasTransformHierarchy: 1 m_AllowConstantClipSamplingOptimization: 1 m_KeepAnimatorControllerStateOnDisable: 0 +--- !u!114 &743703313 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 743703306} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f77f78e8a73964e469daa76d7287e403, type: 3} + m_Name: + m_EditorClassIdentifier: + fovRadius: 10 + Angle: 103 + fovDelay: 0.3 + targetMask: + serializedVersion: 2 + m_Bits: 64 + obstructionMask: + serializedVersion: 2 + m_Bits: 256 + target: {fileID: 0} --- !u!1 &963720380 GameObject: m_ObjectHideFlags: 0 @@ -948,17 +972,21 @@ propertyPath: m_Name value: Player objectReference: {fileID: 0} + - target: {fileID: 7125823908817143911, guid: 0cf11774487a5ee47a41ba8097423209, type: 3} + propertyPath: m_Layer + value: 6 + objectReference: {fileID: 0} - target: {fileID: 7125823908817143917, guid: 0cf11774487a5ee47a41ba8097423209, type: 3} propertyPath: m_RootOrder value: 2 objectReference: {fileID: 0} - target: {fileID: 7125823908817143917, guid: 0cf11774487a5ee47a41ba8097423209, type: 3} propertyPath: m_LocalPosition.x - value: 42 + value: -0 objectReference: {fileID: 0} - target: {fileID: 7125823908817143917, guid: 0cf11774487a5ee47a41ba8097423209, type: 3} propertyPath: m_LocalPosition.y - value: 41.1 + value: -4.9 objectReference: {fileID: 0} - target: {fileID: 7125823908817143917, guid: 0cf11774487a5ee47a41ba8097423209, type: 3} propertyPath: m_LocalPosition.z diff --git a/Assets/Scripts/NPC/FOV.cs b/Assets/Scripts/NPC/FOV.cs new file mode 100644 index 0000000..a37726f --- /dev/null +++ b/Assets/Scripts/NPC/FOV.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FOV : MonoBehaviour +{ + [Header("FOV Settings: ")] + [SerializeField] private float fovRadius; + [Range(0, 360)][SerializeField] private float Angle; + [SerializeField] private float fovDelay; + [SerializeField] private LayerMask targetMask; + [SerializeField] private LayerMask obstructionMask; + + [SerializeField] private GameObject target; + + private NPCConroller controller; + private Agent agent; + + private void Awake() + { + controller = GetComponent(); + } + // Start is called before the first frame update + void Start() + { + agent = controller.Agent; + StartCoroutine(FOVRoutine(fovDelay)); + } + + // Update is called once per frame + void Update() + { + + } + + private void PerformFov() + { + Collider2D collider = Physics2D.OverlapCircle(transform.position, fovRadius, targetMask); + GameObject potentialTarget = null; + + if (collider != null) + { + potentialTarget = collider.gameObject; + Debug.Log($"Collider FOV detected something: {potentialTarget}"); + Vector2 dirToTarget = (Vec3ToVec2(potentialTarget.transform.position) - Vec3ToVec2(transform.position)).normalized; + if (Vector3.Angle(transform.forward, dirToTarget) < Angle/2) + { + Debug.Log("Target in Angle!"); + target = potentialTarget; + //float distanceToTarget = Vector2.Distance(transform.forward, dirToTarget); + + //if(Physics2D.Raycast(transform.position, dirToTarget, distanceToTarget,)) + } + } + } + + private static Vector2 Vec3ToVec2(Vector3 vec) => (Vector2)vec; + + private IEnumerator FOVRoutine(float seconds) + { + WaitForSeconds wait = new WaitForSeconds(seconds); + yield return wait; + PerformFov(); + } + + public GameObject GetTarget() => target; +} diff --git a/Assets/Scripts/NPC/FOV.cs.meta b/Assets/Scripts/NPC/FOV.cs.meta new file mode 100644 index 0000000..753202e --- /dev/null +++ b/Assets/Scripts/NPC/FOV.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f77f78e8a73964e469daa76d7287e403 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/NPCConroller.cs b/Assets/Scripts/NPC/NPCConroller.cs index 583f28d..2d1b2f5 100644 --- a/Assets/Scripts/NPC/NPCConroller.cs +++ b/Assets/Scripts/NPC/NPCConroller.cs @@ -9,9 +9,11 @@ [SerializeField] protected FSM fsm; [SerializeField] protected Agent agent; protected Animator animator; + protected FOV agentFOV; public Agent Agent => agent; public Animator Animator => animator; + public FOV AgentFOV => agentFOV; protected virtual void Awake() { @@ -20,6 +22,9 @@ Debug.LogError("Agent component not found on this GameObject!"); } + if(!TryGetComponent(out agentFOV)) + Debug.LogError($"{nameof(FOV)} component not found on this GameObject!"); + animator = GetComponent(); } diff --git a/Assets/Scripts/NPC/Orc/Actions/ChaseTartet.cs b/Assets/Scripts/NPC/Orc/Actions/ChaseTartet.cs index 3749aee..6399828 100644 --- a/Assets/Scripts/NPC/Orc/Actions/ChaseTartet.cs +++ b/Assets/Scripts/NPC/Orc/Actions/ChaseTartet.cs @@ -10,7 +10,7 @@ { Agent agent = fsm.Controller.Agent; EnemyNPCContoller controller = (EnemyNPCContoller)fsm.Controller; - GameObject target = controller.Target; + GameObject target = controller.AgentFOV.GetTarget(); if (agent != null) { diff --git a/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset new file mode 100644 index 0000000..f3a5e4d --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset @@ -0,0 +1,14 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: af922201547d05946a3579b321667273, type: 3} + m_Name: CanSeeTarget + m_EditorClassIdentifier: diff --git a/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset.meta b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset.meta new file mode 100644 index 0000000..55cde97 --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dc955976ccd89da4895726795cafd50a +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs new file mode 100644 index 0000000..df01807 --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs @@ -0,0 +1,15 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using MyCollections.AI.FinitStateMachine; + +[CreateAssetMenu(menuName = "Finite State Machine/Condition/CanSeeTarget")] +public class CanSeeTarget : Condition +{ + public override bool CheckCondition(FSM fsm) + { + FOV fov = fsm.Controller.AgentFOV; + + return fov.GetTarget() != null ? true : false; + } +} diff --git a/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs.meta b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs.meta new file mode 100644 index 0000000..0091c8e --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/CanSeeTarget.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: af922201547d05946a3579b321667273 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs b/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs new file mode 100644 index 0000000..154d693 --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using MyCollections.AI.FinitStateMachine; + +[CreateAssetMenu(menuName = "Finite State Machine/Condition/TargetDistance")] +public class TargetDistance : Condition +{ + [SerializeField] private float distance; + [SerializeField] private bool targetClose; + + public override bool CheckCondition(FSM fsm) + { + if (targetClose) + { + //if(fsm.Controller.Agent.destination == fsm.agent.) + } + + return false; + } +} diff --git a/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs.meta b/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs.meta new file mode 100644 index 0000000..30e9b9a --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Conditions/TargetDistance.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d3b7a9a5bbe04c4bba8049b04320fc7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/Orc/OrcFSM.asset b/Assets/Scripts/NPC/Orc/OrcFSM.asset index fbc2c8d..9454fef 100644 --- a/Assets/Scripts/NPC/Orc/OrcFSM.asset +++ b/Assets/Scripts/NPC/Orc/OrcFSM.asset @@ -12,6 +12,6 @@ m_Script: {fileID: 11500000, guid: 51f43cc9e43d6a340b860f1495565b51, type: 3} m_Name: OrcFSM m_EditorClassIdentifier: - initialState: {fileID: 11400000, guid: 46e233bbb8a097e45828adee0507c870, type: 2} - currentState: {fileID: 11400000, guid: 46e233bbb8a097e45828adee0507c870, type: 2} + initialState: {fileID: 11400000, guid: f544bbd18c02f304386d6f1c01fb3431, type: 2} + currentState: {fileID: 11400000, guid: f544bbd18c02f304386d6f1c01fb3431, type: 2} target: {fileID: 0} diff --git a/Assets/Scripts/NPC/Orc/States/Idle.asset b/Assets/Scripts/NPC/Orc/States/Idle.asset index 039fd89..9cc4b3b 100644 --- a/Assets/Scripts/NPC/Orc/States/Idle.asset +++ b/Assets/Scripts/NPC/Orc/States/Idle.asset @@ -15,5 +15,6 @@ entryActions: - {fileID: 11400000, guid: 8c27bb2d79d5f0540bfd7faac8760886, type: 2} actions: [] - transitions: [] + transitions: + - {fileID: 11400000, guid: 8e509e9af5720ba4183642f10f0b87d9, type: 2} exitAction: {fileID: 0} diff --git a/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset b/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset new file mode 100644 index 0000000..ceb3a4e --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset @@ -0,0 +1,17 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6af03f2a2f4c7994cb57d6e4a1646605, type: 3} + m_Name: ChaseToMeleeCombat + m_EditorClassIdentifier: + condition: {fileID: 0} + targetState: {fileID: 0} + action: {fileID: 0} diff --git a/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset.meta b/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset.meta new file mode 100644 index 0000000..fe66835 --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Transitions/ChaseToMeleeCombat.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: be70e4e8d6c7b9548a6fe136492d5a63 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset b/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset new file mode 100644 index 0000000..df48b31 --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset @@ -0,0 +1,17 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6af03f2a2f4c7994cb57d6e4a1646605, type: 3} + m_Name: IdleToChase + m_EditorClassIdentifier: + condition: {fileID: 11400000, guid: dc955976ccd89da4895726795cafd50a, type: 2} + targetState: {fileID: 11400000, guid: 46e233bbb8a097e45828adee0507c870, type: 2} + action: {fileID: 0} diff --git a/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset.meta b/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset.meta new file mode 100644 index 0000000..eb0ab9f --- /dev/null +++ b/Assets/Scripts/NPC/Orc/Transitions/IdleToChase.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8e509e9af5720ba4183642f10f0b87d9 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/Pathfinding/Agent.cs b/Assets/Scripts/NPC/Pathfinding/Agent.cs index 68c99b3..310e231 100644 --- a/Assets/Scripts/NPC/Pathfinding/Agent.cs +++ b/Assets/Scripts/NPC/Pathfinding/Agent.cs @@ -19,8 +19,16 @@ [SerializeField] private bool stop; [SerializeField] private Vector2 agentDestination; [SerializeField] private Vector2 moveDirection; + + [SerializeField] private float rDist; public bool agentStop { get => stop; set => stop = value; } public Vector2 agentVelocity => moveDirection; + public float remainingDistance => rDist; + + public Vector2 destination + { + get => agentDestination; set => agentDestination = value; + } private void Awake() { @@ -78,6 +86,17 @@ } } + private void CalculatePathDistance(Vector2[] pathPoints) + { + Vector2 currentPoint = transform.position; + + for (int i = 1; i < pathPoints.Length; i++) + { + rDist = Vector2.Distance(currentPoint, pathPoints[i]); + currentPoint = pathPoints[i]; + } + } + private IEnumerator FollowPath() { bool followingPath = true; @@ -109,6 +128,7 @@ if (followingPath) { + CalculatePathDistance(path.lookPoints); if (pathIndex >= path.slowDownIndex && stoppingDistance > 0) { speedPercent = Mathf.Clamp01(path.turnBoundaries[path.finishLineIndex].DistanceFromPoint(position) / stoppingDistance); diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 5c01146..05fa213 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -30,7 +30,7 @@ - Menu - Water - UI - - Quest + - Player - - Unwalkable -