Advertisement
Shubhra_Sarker

Untitled

Jun 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Sight: Sense {
  6. public int FieldOfView = 45;
  7. public int ViewDistance = 100;
  8.  
  9. private Transform playerTrans;
  10. private Vector3 rayDirection;
  11.  
  12. protected override void Initialize()
  13. {
  14. //Find player position
  15. playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
  16. }
  17.  
  18. // Update is called once per frame
  19. protected override void UpdateSense() {
  20. elapsedTime += Time.deltaTime;
  21.  
  22. // Detect perspective sense if within the detection rate
  23. if (elapsedTime >= detectionRate) DetectAspect();
  24. }
  25.  
  26. //Detect perspective field of view for the AI Character
  27. void DetectAspect() {
  28. RaycastHit hit;
  29.  
  30. //Direction from current position to player position
  31. rayDirection = playerTrans.position -
  32. transform.position;
  33.  
  34. //Check the angle between the AI character's forward
  35. //vector and the direction vector between player and AI
  36. if ((Vector3.Angle(rayDirection, transform.forward)) < FieldOfView) {
  37. // Detect if player is within the field of view
  38. if (Physics.Raycast(transform.position, rayDirection, out hit, ViewDistance))
  39. {
  40. if (hit.transform.tag == "Player")
  41. {
  42. print("Player detected");
  43. }
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement