Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /* TL = Top Left
- * TR = Top Right
- * BL = Bottom Left
- * BR = Bottom Right
- */
- public class Script_v2 : MonoBehaviour {
- // Player Properties
- private GameObject player;
- public Vector3 playerSize;
- private Vector3 playerTransform;
- public Vector3 playerTransformTL;
- public Vector3 playerTransformTR;
- public Vector3 playerTransformBL;
- public Vector3 playerTransformBR;
- private Vector3 newPlayerTransformTL;
- private Vector3 newPlayerTransformTR;
- private Vector3[] playerRaycastPoints;
- // Enemy Properties
- private Vector3 enemyTransformTL;
- private Vector3 enemyTransformTR;
- private Vector3 enemyTransformBL;
- private Vector3 enemyTransformBR;
- public float distance;
- public Vector3 enemySize;
- // Detection Alerts
- public bool alerted;
- public bool alertedLock;
- public bool dead;
- Ray ray;
- RaycastHit hit;
- // Use this for initialization
- void Start () {
- playerRaycastPoints = new Vector3[4];
- distance = 3f;
- player = GameObject.FindGameObjectWithTag ("Player");
- }
- // Update is called once per frame
- void Update () {
- enemyTransformTL = new Vector3 (transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z);
- enemyTransformTR = new Vector3 (transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z);
- enemyTransform_TL_TR ();
- detectionAlert ();
- Reference_Player_Transform_Points ();
- Player_Transform_Points_Detection ();
- }
- void OnDrawGizmos() {
- Gizmos.color = Color.blue;
- Gizmos.DrawWireSphere (new Vector3(transform.position.x - 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
- Gizmos.DrawWireSphere (new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, transform.position.z), distance);
- }
- public void enemyTransform_TL_TR() {
- for (int i = 0; i < 4; i++) {
- double enemyAngleTL = Mathf.Atan2(playerRaycastPoints[i].y - ( transform.position.y + 0.5f ),
- playerRaycastPoints[i].x - ( transform.position.x - 0.5f )) * 180f / 3.14159265f;
- Debug.Log (enemyAngleTL);
- double enemyAngleTR = Mathf.Atan2 (playerRaycastPoints[i].y - (transform.position.y + 0.5f),
- playerRaycastPoints[i].x - (transform.position.x + 0.5f)) * 180f / 3.14159265f;
- Vector3 directionTL = (playerRaycastPoints[i] - enemyTransformTL).normalized;
- Ray rayTL = new Ray(enemyTransformTL, directionTL);
- RaycastHit hitTL;
- Vector3 directionTR = (playerRaycastPoints[i] - enemyTransformTR).normalized;
- Ray rayTR = new Ray (enemyTransformTR, directionTR);
- RaycastHit hitTR;
- //Debug.DrawRay (rayTR.origin, rayTR.direction * distance, Color.yellow);
- if(Physics.Raycast (rayTL, out hitTL, distance)) {
- if((enemyAngleTL > 90 && enemyAngleTL < 180)) {
- Debug.DrawRay (rayTL.origin, rayTL.direction * distance, Color.yellow);
- alerted = true;
- }
- }
- else {
- alerted = false;
- }
- }
- }
- public void detectionAlert() {
- if (alerted == true) {
- gameObject.renderer.material.color = Color.red;
- }
- else {
- gameObject.renderer.material.color = Color.white;
- }
- }
- private void Reference_Player_Transform_Points() {
- playerSize = player.transform.localScale;
- playerTransformTL = new Vector3(player.transform.position.x - (playerSize.x / 2),
- player.transform.position.y + playerSize.y / 2,
- player.transform.position.z);
- playerTransformTR = new Vector3(player.transform.position.x + (playerSize.x / 2),
- player.transform.position.y + playerSize.y / 2,
- player.transform.position.z);
- playerTransformBL = new Vector3(player.transform.position.x - (playerSize.x / 2),
- player.transform.position.y - playerSize.y / 2,
- player.transform.position.z);
- playerTransformBR = new Vector3(player.transform.position.x + (playerSize.x / 2),
- player.transform.position.y - playerSize.y / 2,
- player.transform.position.z);
- playerRaycastPoints [0] = playerTransformTL;
- playerRaycastPoints [1] = playerTransformTR;
- playerRaycastPoints [2] = playerTransformBL;
- playerRaycastPoints [3] = playerTransformBR;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement