Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created November 12, 2014 04:33
Show Gist options
  • Save grimmdev/76668ee44bf7c62bdc26 to your computer and use it in GitHub Desktop.
Save grimmdev/76668ee44bf7c62bdc26 to your computer and use it in GitHub Desktop.
Unity Cone of Sight
public static bool Sight (Vector3 target, Vector3 myPos, Vector3 myViewDir, float myViewDist, float myViewAng) {
bool res = false;
float ang = Vector2.Angle(DirGet2D(myPos, target), VecGet2D(myViewDir));
if (ang <= (myViewAng/2f)) {
if (DistGet2D(myPos, target) < myViewDist)
res = true;
}
return res;
}
public static Vector2 VecGet2D (Vector3 v) {
return new Vector2(v.x, v.z);
}
public static Vector2 DirGet2D (Vector3 v1, Vector3 v2) {
return (VecGet2D(v2) - VecGet2D(v1)).normalized;
}
public static float DistGet2D (Vector3 v1, Vector3 v2) {
return Vector2.Distance(VecGet2D(v1), VecGet2D(v2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment