Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /* TL = Top Left
  5. * TR = Top Right
  6. * BL = Bottom Left
  7. * BR = Bottom Right
  8. */
  9.  
  10. public class Script_v2 : MonoBehaviour {
  11.  
  12. // Player Properties
  13. private GameObject player;
  14. public Vector3 playerSize;
  15. private Vector3 playerTransform;
  16. public Vector3 playerTransformTL;
  17. public Vector3 playerTransformTR;
  18. public Vector3 playerTransformBL;
  19. public Vector3 playerTransformBR;
  20.  
  21. private Vector3 newPlayerTransformTL;
  22. private Vector3 newPlayerTransformTR;
  23.  
  24. private Vector3[] playerRaycastPoints;
  25.  
  26.  
  27. // Enemy Properties
  28. private Vector3 enemyTransformTL;
  29. private Vector3 enemyTransformTR;
  30. private Vector3 enemyTransformBL;
  31. private Vector3 enemyTransformBR;
  32.  
  33. public float distance;
  34. public Vector3 enemySize;
  35.  
  36. // Detection Alerts
  37. public bool alerted;
  38. public bool alertedLock;
  39. public bool dead;
  40.  
  41. Ray ray;
  42. RaycastHit hit;
  43.  
  44. // Use this for initialization
  45. void Start () {
  46. playerRaycastPoints = new Vector3[4];
  47.  
  48. distance = 3f;
  49. player = GameObject.FindGameObjectWithTag ("Player");
  50.  
  51.  
  52. }
  53.  
  54. // Update is called once per frame
  55. void Update () {
  56.  
  57. enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
  58. enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);
  59.  
  60.  
  61. enemyTransform_TL_TR ();
  62. detectionAlert ();
  63. Reference_Player_Transform_Points ();
  64. Player_Transform_Points_Detection ();
  65.  
  66.  
  67.  
  68. }
  69.  
  70. void OnDrawGizmos() {
  71. Gizmos.color = Color.blue;
  72. Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
  73. Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
  74. }
  75.  
  76. public void enemyTransform_TL_TR() {
  77.  
  78. for (int i = 0; i < 4; i++) {
  79.  
  80. double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
  81. playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
  82. Debug.Log (enemyAngleTL);
  83. double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
  84. playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
  85.  
  86. Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
  87. Ray rayTL = new Ray(enemyTransformTL, directionTL);
  88. RaycastHit hitTL;
  89. Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
  90. Ray rayTR = new Ray (enemyTransformTR, directionTR);
  91. RaycastHit hitTR;
  92.  
  93.  
  94. //Debug.DrawRay (rayTR.origin, rayTR.direction * distance, Color.yellow);
  95.  
  96. if(Physics.Raycast (rayTL, out hitTL, distance)) {
  97. if((enemyAngleTL > 90 && enemyAngleTL < 180)) {
  98. Debug.DrawRay (rayTL.origin, rayTL.direction * distance, Color.yellow);
  99. alerted = true;
  100.  
  101. }
  102.  
  103. }
  104. else {
  105. alerted = false;
  106. }
  107.  
  108.  
  109. }
  110.  
  111.  
  112. }
  113.  
  114. public void detectionAlert() {
  115. if (alerted == true) {
  116. gameObject.renderer.material.color = Color.red;
  117. }
  118. else {
  119. gameObject.renderer.material.color = Color.white;
  120. }
  121. }
  122.  
  123. private void Reference_Player_Transform_Points() {
  124.  
  125. playerSize = player.transform.localScale;
  126.  
  127. playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
  128. player.transform.position.y + playerSize.y / 2,
  129. player.transform.position.z);
  130. playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
  131. player.transform.position.y + playerSize.y / 2,
  132. player.transform.position.z);
  133. playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
  134. player.transform.position.y - playerSize.y / 2,
  135. player.transform.position.z);
  136. playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
  137. player.transform.position.y - playerSize.y / 2,
  138. player.transform.position.z);
  139.  
  140. playerRaycastPoints [0] = playerTransformTL;
  141. playerRaycastPoints [1] = playerTransformTR;
  142. playerRaycastPoints [2] = playerTransformBL;
  143. playerRaycastPoints [3] = playerTransformBR;
  144.  
  145. }
  146.  
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement