-
-
Save PappaBjorn/b06781ea9db55aa01c841fb49b235fd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Assertions; | |
public class AIPerception : MonoBehaviour | |
{ | |
public float perceptionRadius = 1f; | |
[Range(0, 360f)] public float fovAngle = 100f; | |
public LayerMask perceptionMask; | |
public Material seenMaterial; | |
public Material unseenMaterial; | |
public List<Collider> previouslySeen = new List<Collider>(); | |
public AIStateMachine aiStateMachine; | |
private void Awake() | |
{ | |
aiStateMachine = GetComponent<AIStateMachine>(); | |
Assert.IsNotNull(aiStateMachine, "<b>AIPerception</b> failed to locate <b>AIStateMachine</b> on AI GameObject"); | |
} | |
private void Update() | |
{ | |
for (int i = 0; i < previouslySeen.Count; i++) | |
{ | |
// material renderer for sphere above player. Debug reasons only! | |
previouslySeen[i].GetComponent<Renderer>().sharedMaterial = unseenMaterial; | |
aiStateMachine.seeingObject = false; | |
} | |
previouslySeen.Clear(); | |
previouslySeen.AddRange(Physics.OverlapSphere(transform.position, perceptionRadius, perceptionMask, QueryTriggerInteraction.Ignore)); | |
for (int i = 0; i < previouslySeen.Count; i++) | |
{ | |
Vector3 direction = previouslySeen[i].transform.position - transform.position; | |
float angle = Vector3.Angle(direction, transform.forward); | |
if (angle <= fovAngle * 0.5f) | |
{ | |
// material renderer for sphere above player. Debug reasons only! | |
previouslySeen[i].GetComponent<Renderer>().sharedMaterial = seenMaterial; | |
aiStateMachine.seeingObject = true; | |
} | |
} | |
} | |
#if UNITY_EDITOR | |
private void OnDrawGizmosSelected() | |
{ | |
if (!Application.isPlaying) | |
{ | |
Color oldColor = Gizmos.color; | |
float halfFOV = fovAngle * 0.5f; | |
float coneDir = -90f; | |
Quaternion leftRayRotation = Quaternion.AngleAxis(-halfFOV + coneDir, Vector3.up); | |
Quaternion rightRayRotation = Quaternion.AngleAxis(halfFOV + coneDir, Vector3.up); | |
Vector3 leftDir = leftRayRotation * transform.right * perceptionRadius; | |
Vector3 rightDir = rightRayRotation * transform.right * perceptionRadius; | |
UnityEditor.Handles.color = new Color(0f, 1f, 0f, 0.15f); | |
UnityEditor.Handles.DrawSolidArc(transform.position, Vector3.up, leftDir, fovAngle, perceptionRadius); | |
UnityEditor.Handles.color = new Color(1f, 0f, 0f, 0.15f); | |
UnityEditor.Handles.DrawSolidArc(transform.position, Vector3.up, rightDir, 360f - fovAngle, perceptionRadius); | |
Gizmos.color = oldColor; | |
} | |
} | |
#endif | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class AIStateMachine : MonoBehaviour | |
{ | |
public bool patrolling = false; | |
public bool stalking = false; | |
public bool retreating = false; | |
public bool idle = false; | |
public bool attacking = false; | |
public bool charging = false; | |
public bool seeingObject = false; | |
public bool stunned = false; | |
public void SetPreviousStateFalse() | |
{ | |
patrolling = false; | |
stalking = false; | |
retreating = false; | |
idle = false; | |
attacking = false; | |
charging = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment