Advertisement
Guest User

FOV3D

a guest
Apr 16th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. // NOTE FOR THE PUBLIC: ACTUALLY ID DID'T WORK AS EXPECTED !
  2. // Conversion from https://gist.github.com/naruse/97f88fa9f7b530349518
  3. // Website Reference: http://www.pencilsquaregames.com/2014/09/light-easy-and-fast-field-of-view-2d/
  4.  
  5. using UnityEngine;
  6. using System.Collections;
  7.  
  8. public class AiBehaviour : MonoBehaviour
  9. {
  10.     public float DistanceView = 25f;
  11.     public bool DebugRay = false;
  12.     public float ViewDelay = 1;
  13.     private float viewDelayTime = 0;
  14.  
  15.  
  16.     [Range(0.1f, 10f)]
  17.     public float radius = 1;
  18.  
  19.     [Range(1.0f, 360f)]
  20.     public int fov = 90;//90 degrees
  21.  
  22.     public Vector3 direction = Vector3.forward;
  23.  
  24.     //used to test the FOV
  25.     public Transform testPoint;
  26.  
  27.     private Vector3 leftLineFOV;
  28.     private Vector3 rightLineFOV;
  29.  
  30.  
  31.     void Update()
  32.     {
  33.         viewDelayTime += Time.deltaTime;
  34.  
  35.     }
  36.  
  37.     void FixedUpdate()
  38.     {
  39.         if (viewDelayTime > ViewDelay)
  40.         {
  41.             direction = transform.TransformDirection(Vector3.forward);
  42.             // WhatAiView();
  43.             // viewDelayTime = 0;
  44.             if (testPoint != null)
  45.             {
  46.                 rightLineFOV = RotatePointAroundTransform(direction.normalized * radius, -fov / 2);
  47.                 leftLineFOV = RotatePointAroundTransform(direction.normalized * radius, fov / 2);
  48.                 Debug.Log("Debug::"+ InsideFOV(new Vector3(testPoint.position.x, 0 ,testPoint.position.z)));
  49.             }
  50.         }
  51.     }
  52.  
  53.     private void WhatAiView()
  54.     {
  55.         RaycastHit hit;
  56.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
  57.         if (DebugRay)
  58.             Debug.DrawRay(transform.position, fwd * DistanceView, Color.green);
  59.  
  60.         if (Physics.Raycast(transform.position, fwd, out hit, DistanceView, 9))
  61.         {
  62.             // if Hit is a terrain, Ignore it !
  63.             if (hit.transform.gameObject == gameObject.GetComponent<Terrain>())
  64.                 return;
  65.  
  66.             // if the Hit Object won the same Player, it's the same team, Ignore it!
  67.             if (hit.transform.gameObject.GetComponent<Player>().Info.Name ==
  68.                 gameObject.GetComponent<Player>().Info.Name)
  69.                 return;
  70.  
  71.             //Yo! Here we B!
  72.                 if (DebugRay)
  73.                     Debug.Log(hit.transform.name + " Spoted!");
  74.         }
  75.     }
  76.  
  77.     //Rotate point (px, pz) around point (ox, oz) by angle theta you'll get:
  78.     //p'x = cos(theta) * (px-ox) - sin(theta) * (pz-oz) + ox
  79.     //p'z = sin(theta) * (px-ox) + cos(theta) * (pz-oy) + oz
  80.     private Vector3 RotatePointAroundTransform(Vector3 p, float angles)
  81.     {
  82.         return new Vector3(Mathf.Cos((angles) * Mathf.Deg2Rad) * (p.x) - Mathf.Sin((angles) * Mathf.Deg2Rad) * (p.z),
  83.                            0,
  84.                            Mathf.Sin((angles) * Mathf.Deg2Rad) * (p.x) + Mathf.Cos((angles) * Mathf.Deg2Rad) * (p.y));
  85.     }
  86.  
  87.     public bool InsideFOV(Vector3 playerPos)
  88.     {
  89.         direction = transform.TransformDirection(Vector3.forward);
  90.  
  91.         float squaredDistance = ((playerPos.x - transform.position.x) * (playerPos.x - transform.position.x)) + ((playerPos.z - transform.position.z) * (playerPos.z - transform.position.z));
  92.         Debug.Log(squaredDistance);
  93.         if (radius * radius >= squaredDistance)
  94.         {
  95.             float signLeftLine = (leftLineFOV.x) * (playerPos.z - transform.position.z) - (leftLineFOV.z) * (playerPos.x - transform.position.x);
  96.             float signRightLine = (rightLineFOV.x) * (playerPos.z - transform.position.z) - (rightLineFOV.z) * (playerPos.x - transform.position.x);
  97.             if (fov <= 180)
  98.             {
  99.                 Debug.Log(signLeftLine + " " + signRightLine);
  100.                 if (signLeftLine <= 0 && signRightLine >= 0)
  101.                     return true;
  102.             }
  103.             else {
  104.                 if (!(signLeftLine >= 0 && signRightLine <= 0))
  105.                     return true;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     void OnDrawGizmos()
  112.     {
  113.         Gizmos.color = Color.green;
  114.         Gizmos.DrawRay(transform.position, direction.normalized * radius);
  115.  
  116.         rightLineFOV = RotatePointAroundTransform(direction.normalized * radius, -fov / 2);
  117.         leftLineFOV = RotatePointAroundTransform(direction.normalized * radius, fov / 2);
  118.  
  119.         Gizmos.color = Color.yellow;
  120.         Gizmos.DrawRay(transform.position, rightLineFOV);
  121.         Gizmos.DrawRay(transform.position, leftLineFOV);
  122.  
  123.         Vector3 p = rightLineFOV;
  124.         for (int i = 1; i <= 20; i++)
  125.         {
  126.             float step = fov / 20;
  127.             Vector3 p1 = RotatePointAroundTransform(direction.normalized * radius, -fov / 2 + step * (i));
  128.             Gizmos.DrawRay(new Vector3(transform.position.x, 0, transform.position.z) + p, p1 - p);
  129.             p = p1;
  130.         }
  131.         Gizmos.DrawRay(new Vector3(transform.position.x, 0, transform.position.z) + p, leftLineFOV - p);
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement