Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Enemy: MonoBehaviour {
- public enum weaponRange {
- Melee,
- Ranged
- }
- public enum texturePosition {
- Up,
- Right,
- Down,
- Left
- }
- public float Health = 100; //Enemy health
- public weaponRange Type; //Type of weapon it carries
- public string PlayerTag = "Player"; // Tag to attack
- [Header("Detecting Player Options")]
- [Range(0f, 360f)] public float ViewConeAngle = 60; //Max FOV of enemy
- public float Distance = 3f; //Max distance it can see a player
- public texturePosition TextureFacing; //Initial texture facing
- void Update() {
- GameObject player = GameObject.FindWithTag(PlayerTag);
- if(!ObjectsInterfering(player) && VisionCone(player)) {
- print ("te veo");
- }
- else {
- print ("no te veo");
- }
- }
- bool VisionCone(GameObject target) {
- Vector2 forward;
- Vector2 transformVector = new Vector2();
- float angle;
- //Debug Vars
- Vector2 coneR = new Vector2();
- Vector2 coneL = new Vector2();
- float radians = (ViewConeAngle / 2) * Mathf.Deg2Rad;
- switch(TextureFacing.ToString()) {
- case "Up":
- transformVector = Vector2.up;
- //Debug
- coneR = transform.TransformDirection(new Vector2(Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
- coneL = transform.TransformDirection(new Vector2(- Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
- break;
- case "Right":
- transformVector = Vector2.right;
- //Debug
- coneR = transform.TransformDirection(new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * Distance);
- coneL = transform.TransformDirection(new Vector2(Mathf.Cos(radians), - Mathf.Sin(radians)) * Distance);
- break;
- case "Down":
- transformVector = Vector2.down;
- //Debug
- coneR = - transform.TransformDirection(new Vector2(Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
- coneL = - transform.TransformDirection(new Vector2(- Mathf.Sin(radians), Mathf.Cos(radians)) * Distance);
- break;
- case "Left":
- transformVector = Vector2.left;
- //Debug
- coneR = - transform.TransformDirection(new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * Distance);
- coneL = - transform.TransformDirection(new Vector2(Mathf.Cos(radians), - Mathf.Sin(radians)) * Distance);
- break;
- }
- forward = transform.TransformDirection(transformVector * Distance); //forward direction from selected face
- angle = Vector2.Angle(target.transform.position - transform.position, forward); //angle between player and enemy
- //Only for debug purposes
- Debug.DrawRay (transform.position, forward, Color.red); //forward direction
- Debug.DrawRay (transform.position, coneR, Color.blue); //vision cone right
- Debug.DrawRay (transform.position, coneL, Color.blue); //vision cone left
- if(angle <= (ViewConeAngle / 2)) { //if angle is less or equal to half enemy FOV you are watching enemy
- return true;
- }
- else {
- return false;
- }
- }
- bool ObjectsInterfering (GameObject target) {
- RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position, Distance);
- Debug.DrawRay(transform.position, target.transform.position, Color.green);
- if(hit.transform.tag == target.tag) {
- return true;
- }
- else {
- return false;
- }
- }
- void Focus() {
- GameObject[] targets = GameObject.FindGameObjectsWithTag(PlayerTag);
- float[] distance = new float[targets.Length];
- for(int i = 0; i < targets.Length; i ++) {
- distance[i] = Vector3.Distance(targets[i].transform.position, transform.position);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement