Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Enemy: MonoBehaviour {
  5.  
  6. public enum weaponRange {
  7. Melee,
  8. Ranged
  9. }
  10.  
  11. public enum texturePosition {
  12. Up,
  13. Right,
  14. Down,
  15. Left
  16. }
  17.  
  18. public float Health = 100; //Enemy health
  19. public weaponRange Type; //Type of weapon it carries
  20. public string PlayerTag = "Player"; // Tag to attack
  21.  
  22. [Header("Detecting Player Options")]
  23. [Range(0f, 360f)] public float ViewConeAngle = 60; //Max FOV of enemy
  24. public float Distance = 3f; //Max distance it can see a player
  25. public texturePosition TextureFacing; //Initial texture facing
  26.  
  27. void Update() {
  28. GameObject player = GameObject.FindWithTag(PlayerTag);
  29. if(!ObjectsInterfering(player) && VisionCone(player)) {
  30. print ("te veo");
  31. }
  32. else {
  33. print ("no te veo");
  34. }
  35. }
  36.  
  37. bool VisionCone(GameObject target) {
  38.  
  39. Vector2 forward;
  40. Vector2 transformVector = new Vector2();
  41. float angle;
  42.  
  43. //Debug Vars
  44. Vector2 coneR = new Vector2();
  45. Vector2 coneL = new Vector2();
  46. float radians = (ViewConeAngle / 2) * Mathf.Deg2Rad;
  47.  
  48. switch(TextureFacing.ToString()) {
  49. case "Up":
  50. transformVector = Vector2.up;
  51.  
  52. //Debug
  53. coneR = transform.TransformDirection(new Vector2(Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
  54. coneL = transform.TransformDirection(new Vector2(- Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
  55. break;
  56.  
  57. case "Right":
  58. transformVector = Vector2.right;
  59.  
  60. //Debug
  61. coneR = transform.TransformDirection(new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * Distance);
  62. coneL = transform.TransformDirection(new Vector2(Mathf.Cos(radians), - Mathf.Sin(radians)) * Distance);
  63. break;
  64.  
  65. case "Down":
  66. transformVector = Vector2.down;
  67.  
  68. //Debug
  69. coneR = - transform.TransformDirection(new Vector2(Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
  70. coneL = - transform.TransformDirection(new Vector2(- Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
  71. break;
  72.  
  73. case "Left":
  74. transformVector = Vector2.left;
  75.  
  76. //Debug
  77. coneR = - transform.TransformDirection(new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * Distance);
  78. coneL = - transform.TransformDirection(new Vector2(Mathf.Cos(radians), - Mathf.Sin(radians)) * Distance);
  79. break;
  80. }
  81.  
  82. forward = transform.TransformDirection(transformVector * Distance); //forward direction from selected face
  83. angle = Vector2.Angle(target.transform.position - transform.position, forward); //angle between player and enemy
  84.  
  85. //Only for debug purposes
  86. Debug.DrawRay (transform.position, forward, Color.red); //forward direction
  87. Debug.DrawRay (transform.position, coneR, Color.blue); //vision cone right
  88. Debug.DrawRay (transform.position, coneL, Color.blue); //vision cone left
  89.  
  90. if(angle <= (ViewConeAngle / 2)) { //if angle is less or equal to half enemy FOV you are watching enemy
  91. return true;
  92. }
  93. else {
  94. return false;
  95. }
  96. }
  97.  
  98. bool ObjectsInterfering (GameObject target) {
  99. RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position, Distance);
  100. Debug.DrawRay(transform.position, target.transform.position, Color.green);
  101.  
  102. if(hit.transform.tag == target.tag) {
  103. return true;
  104. }
  105.  
  106. else {
  107. return false;
  108. }
  109.  
  110. }
  111.  
  112. void Focus() {
  113. GameObject[] targets = GameObject.FindGameObjectsWithTag(PlayerTag);
  114. float[] distance = new float[targets.Length];
  115.  
  116. for(int i = 0; i < targets.Length; i ++) {
  117. distance[i] = Vector3.Distance(targets[i].transform.position, transform.position);
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement