Advertisement
talmud

Detecting / facing target

Nov 2nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8.  
  9. public class EnemyAI : MonoBehaviour {
  10.  
  11. public float visualDetectionRadius;
  12. public float audioDetectionRadius;
  13. public float peripheral;
  14.  
  15. public float turnSpeed = 1f;
  16. public float speed = 1f;
  17.  
  18. // Use this for initialization
  19. void Start () {
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update ()
  24. {
  25. SpotTarget();
  26.  
  27. HearTarget();
  28.  
  29. }
  30.  
  31. void SpotTarget()
  32. {
  33. Collider[] colliders = Physics.OverlapSphere(transform.position, visualDetectionRadius);
  34.  
  35. foreach (Collider collider in colliders)
  36. {
  37. // check if target is player
  38. if (collider.CompareTag("Player"))
  39. {
  40. //transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, collider.transform.position, turnSpeed, 0f));
  41. //turn towards the player
  42. float playerDirection = transform.position.AngleToFaceTarget(collider.transform.position);
  43.  
  44. float myFacing = transform.eulerAngles.y;
  45.  
  46. if (Mathf.DeltaAngle(myFacing, playerDirection) <= peripheral)
  47. {
  48. RaycastHit hit;
  49. if (Physics.Raycast(transform.position, transform.forward, out hit)
  50. && hit.transform.CompareTag("Player"))
  51. {
  52. Charge();
  53. }
  54. LookAtTarget(collider.transform);
  55.  
  56.  
  57. // checking if object exists in front of us && object is tagged player
  58.  
  59.  
  60. }
  61. }
  62. }
  63. }
  64.  
  65. void HearTarget()
  66. {
  67. Collider[] colliders = Physics.OverlapSphere(transform.position, audioDetectionRadius);
  68.  
  69. foreach (Collider collider in colliders)
  70. {
  71. // check if target is player
  72. if (collider.CompareTag("Player"))
  73. {
  74. LookAtTarget(collider.transform);
  75. }
  76. }
  77. }
  78.  
  79. void LookAtTarget(Transform target)
  80. {
  81. //transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, collider.transform.position, turnSpeed, 0f));
  82. //turn towards the player
  83. float angleToTurn = transform.position.AngleToFaceTarget(target.position);
  84.  
  85.  
  86. Quaternion newRotation = Quaternion.Euler(0, angleToTurn, 0);
  87. transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 120f * Time.deltaTime);
  88. }
  89.  
  90. private void Charge()
  91. {
  92.  
  93. // moving forward 1m / second
  94. transform.position += transform.forward * speed* Time.deltaTime;
  95. }
  96.  
  97. #if UNITY_EDITOR
  98. void OnDrawGizmos()
  99. {
  100. Gizmos.color = Color.black;
  101. Gizmos.DrawWireSphere(transform.position, visualDetectionRadius);
  102. Gizmos.color = Color.red;
  103. Gizmos.DrawWireSphere(transform.position, audioDetectionRadius);
  104.  
  105. //Vector3 leftPeripheralTarget = transform.rot(Vector3.forward * visualDetectionRadius).
  106. //Gizmos.DrawRay(transform.position, ()
  107. }
  108. #endif
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement