Created
November 10, 2019 16:31
-
-
Save UtMan88/39ccf0d7b7786fca7b945f34143010b6 to your computer and use it in GitHub Desktop.
Create an array of points to be used as turrets, and detect if a point is within a certain angle range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ArcDetect : MonoBehaviour | |
{ | |
[System.Serializable] | |
public class Turret | |
{ | |
[SerializeField] | |
Vector3 _localPosition; | |
[SerializeField] | |
float _angleMin = -90; | |
[SerializeField] | |
float _angleMax = 90; | |
public float Range = 10f; | |
public float AngleMin(Transform t) | |
{ | |
return _angleMin + t.eulerAngles.y; | |
} | |
public float AngleMax(Transform t) | |
{ | |
return _angleMax + t.eulerAngles.y; | |
} | |
public Vector3 Position(Transform t) | |
{ | |
return t.position + _localPosition; | |
} | |
public Vector3 Forward(Transform t) | |
{ | |
var position = Position(t); | |
float aMin = AngleMin(t); | |
float aMax = AngleMax(t); | |
Vector3 vMin = new Vector3(Mathf.Sin(aMin * Mathf.Deg2Rad), 0, Mathf.Cos(aMin * Mathf.Deg2Rad)); | |
Vector3 vMax = new Vector3(Mathf.Sin(aMax * Mathf.Deg2Rad), 0, Mathf.Cos(aMax * Mathf.Deg2Rad)); | |
return ((vMin + vMax) * 0.5f).normalized; | |
} | |
public Plane MinPlane(Transform t) | |
{ | |
var position = Position(t); | |
float aMin = AngleMin(t); | |
Vector3 vMin = new Vector3(Mathf.Sin(aMin * Mathf.Deg2Rad), 0, Mathf.Cos(aMin * Mathf.Deg2Rad)); | |
Vector3 normal = Vector3.Cross(vMin, Vector3.down); | |
return new Plane(normal, position); | |
} | |
public Plane MaxPlane(Transform t) | |
{ | |
var position = Position(t); | |
float aMax = AngleMax(t); | |
Vector3 vMax = new Vector3(Mathf.Sin(aMax * Mathf.Deg2Rad), 0, Mathf.Cos(aMax * Mathf.Deg2Rad)); | |
Vector3 normal = Vector3.Cross(vMax, Vector3.up); | |
return new Plane(normal, position); | |
} | |
public bool IsInRange(Transform t, Vector3 point) | |
{ | |
var position = Position(t); | |
float dist = Vector3.Distance(position, point); | |
if(dist <= Range) | |
{ | |
var minP = MinPlane(t); | |
var maxP = MaxPlane(t); | |
var direction = point - position; | |
if(Vector3.Dot(minP.normal, direction) > 0 || Vector3.Dot(maxP.normal, direction) > 0) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
public Ship ship; | |
public Turret[] turrets; | |
public Transform test; | |
public void OnDrawGizmos() | |
{ | |
const int segments = 5; | |
List<Vector2> arcPoints = new List<Vector2>(); | |
float angle; | |
float arcLength; | |
Vector3 position; | |
foreach (Turret t in turrets) | |
{ | |
position = t.Position(transform); | |
angle = t.AngleMin(transform); | |
arcLength = t.AngleMax(transform) - angle; | |
for (int i = 0; i <= segments; i++) | |
{ | |
float x = Mathf.Sin(Mathf.Deg2Rad * angle) * t.Range; | |
float y = Mathf.Cos(Mathf.Deg2Rad * angle) * t.Range; | |
arcPoints.Add(new Vector2(x, y)); | |
angle += (arcLength / segments); | |
} | |
Gizmos.color = Color.red; | |
var arcPos = new Vector3(position.x + arcPoints[0].x, position.y, position.z + arcPoints[0].y); | |
var prevPos = arcPos; | |
Gizmos.DrawLine(position, arcPos); | |
for(int i = 0; i < arcPoints.Count; ++i) | |
{ | |
arcPos = new Vector3(position.x + arcPoints[i].x, position.y, position.z + arcPoints[i].y); | |
Gizmos.DrawLine(prevPos, arcPos); | |
prevPos = arcPos; | |
} | |
arcPos = new Vector3(position.x + arcPoints[arcPoints.Count - 1].x, position.y, position.z + arcPoints[arcPoints.Count - 1].y); | |
Gizmos.DrawLine(position, arcPos); | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
// For testing purposes | |
foreach (Turret t in turrets) | |
{ | |
if(t.IsInRange(transform, test.position)) | |
{ | |
Debug.DrawLine(t.Position(transform), test.position, Color.cyan); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment