Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOTE FOR THE PUBLIC: ACTUALLY ID DID'T WORK AS EXPECTED !
- // Conversion from https://gist.github.com/naruse/97f88fa9f7b530349518
- // Website Reference: http://www.pencilsquaregames.com/2014/09/light-easy-and-fast-field-of-view-2d/
- using UnityEngine;
- using System.Collections;
- public class AiBehaviour : MonoBehaviour
- {
- public float DistanceView = 25f;
- public bool DebugRay = false;
- public float ViewDelay = 1;
- private float viewDelayTime = 0;
- [Range(0.1f, 10f)]
- public float radius = 1;
- [Range(1.0f, 360f)]
- public int fov = 90;//90 degrees
- public Vector3 direction = Vector3.forward;
- //used to test the FOV
- public Transform testPoint;
- private Vector3 leftLineFOV;
- private Vector3 rightLineFOV;
- void Update()
- {
- viewDelayTime += Time.deltaTime;
- }
- void FixedUpdate()
- {
- if (viewDelayTime > ViewDelay)
- {
- direction = transform.TransformDirection(Vector3.forward);
- // WhatAiView();
- // viewDelayTime = 0;
- if (testPoint != null)
- {
- rightLineFOV = RotatePointAroundTransform(direction.normalized * radius, -fov / 2);
- leftLineFOV = RotatePointAroundTransform(direction.normalized * radius, fov / 2);
- Debug.Log("Debug::"+ InsideFOV(new Vector3(testPoint.position.x, 0 ,testPoint.position.z)));
- }
- }
- }
- private void WhatAiView()
- {
- RaycastHit hit;
- Vector3 fwd = transform.TransformDirection(Vector3.forward);
- if (DebugRay)
- Debug.DrawRay(transform.position, fwd * DistanceView, Color.green);
- if (Physics.Raycast(transform.position, fwd, out hit, DistanceView, 9))
- {
- // if Hit is a terrain, Ignore it !
- if (hit.transform.gameObject == gameObject.GetComponent<Terrain>())
- return;
- // if the Hit Object won the same Player, it's the same team, Ignore it!
- if (hit.transform.gameObject.GetComponent<Player>().Info.Name ==
- gameObject.GetComponent<Player>().Info.Name)
- return;
- //Yo! Here we B!
- if (DebugRay)
- Debug.Log(hit.transform.name + " Spoted!");
- }
- }
- //Rotate point (px, pz) around point (ox, oz) by angle theta you'll get:
- //p'x = cos(theta) * (px-ox) - sin(theta) * (pz-oz) + ox
- //p'z = sin(theta) * (px-ox) + cos(theta) * (pz-oy) + oz
- private Vector3 RotatePointAroundTransform(Vector3 p, float angles)
- {
- return new Vector3(Mathf.Cos((angles) * Mathf.Deg2Rad) * (p.x) - Mathf.Sin((angles) * Mathf.Deg2Rad) * (p.z),
- 0,
- Mathf.Sin((angles) * Mathf.Deg2Rad) * (p.x) + Mathf.Cos((angles) * Mathf.Deg2Rad) * (p.y));
- }
- public bool InsideFOV(Vector3 playerPos)
- {
- direction = transform.TransformDirection(Vector3.forward);
- float squaredDistance = ((playerPos.x - transform.position.x) * (playerPos.x - transform.position.x)) + ((playerPos.z - transform.position.z) * (playerPos.z - transform.position.z));
- Debug.Log(squaredDistance);
- if (radius * radius >= squaredDistance)
- {
- float signLeftLine = (leftLineFOV.x) * (playerPos.z - transform.position.z) - (leftLineFOV.z) * (playerPos.x - transform.position.x);
- float signRightLine = (rightLineFOV.x) * (playerPos.z - transform.position.z) - (rightLineFOV.z) * (playerPos.x - transform.position.x);
- if (fov <= 180)
- {
- Debug.Log(signLeftLine + " " + signRightLine);
- if (signLeftLine <= 0 && signRightLine >= 0)
- return true;
- }
- else {
- if (!(signLeftLine >= 0 && signRightLine <= 0))
- return true;
- }
- }
- return false;
- }
- void OnDrawGizmos()
- {
- Gizmos.color = Color.green;
- Gizmos.DrawRay(transform.position, direction.normalized * radius);
- rightLineFOV = RotatePointAroundTransform(direction.normalized * radius, -fov / 2);
- leftLineFOV = RotatePointAroundTransform(direction.normalized * radius, fov / 2);
- Gizmos.color = Color.yellow;
- Gizmos.DrawRay(transform.position, rightLineFOV);
- Gizmos.DrawRay(transform.position, leftLineFOV);
- Vector3 p = rightLineFOV;
- for (int i = 1; i <= 20; i++)
- {
- float step = fov / 20;
- Vector3 p1 = RotatePointAroundTransform(direction.normalized * radius, -fov / 2 + step * (i));
- Gizmos.DrawRay(new Vector3(transform.position.x, 0, transform.position.z) + p, p1 - p);
- p = p1;
- }
- Gizmos.DrawRay(new Vector3(transform.position.x, 0, transform.position.z) + p, leftLineFOV - p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement