public abstract class UnitBase : GameObjectBase { public List<Transform> visiblePoints; ... }
public static bool IsVisibleUnit<T>(T unit, Transform from, float angle, float distance, LayerMask mask) where T : UnitBase { bool result = false; if (unit != null) { foreach (Transform visiblePoint in unit.visiblePoints) { if (IsVisibleObject(from, visiblePoint.position, unit.gameObject, angle, distance, mask)) { result = true; break; } } } return result; }
public static bool IsVisibleObject(Transform from, Vector3 point, GameObject target, float angle, float distance, LayerMask mask) { bool result = false; if (IsAvailablePoint(from, point, angle, distance)) { Vector3 direction = (point - from.position); Ray ray = new Ray(from.position, direction); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance, mask.value)) { if (hit.collider.gameObject == target) { result = true; } } } return result; }
public static bool IsAvailablePoint(Transform from, Vector3 point, float angle, float distance) { bool result = false; if (from != null && Vector3.Distance(from.position, point) <= distance) { Vector3 direction = (point - from.position); float dot = Vector3.Dot(from.forward, direction.normalized); if (dot < 1) { float angleRadians = Mathf.Acos(dot); float angleDeg = angleRadians * Mathf.Rad2Deg; result = (angleDeg <= angle); } else { result = true; } } return result; }
public virtual void ApplyNoise(Vector3 target, float radius, NoiseType type) { List<AIHearlingBase> aiObjects = AIManager.Instance.GetAIObjects<AIHearlingBase>(); foreach (AIHearlingBase ai in aiObjects) { if(Vector3.Distance(ai.unit.Position, target) <= ai.hearingRadius + radius) { ai.ApplyHearling(target, type); } } }
public virtual List<T> GetVisibleUnits<T>(Comparer<T> comparer) where T : UnitBase { List<T> result = new List<T>(); foreach (T unit in UnitsManager.Instance.GetUnits<T>()) { if (unit != null && unit != this.unit && unit.enabled && comparer(unit) && IsVisibleUnit(unit)) { result.Add(unit); } } return result; } public virtual bool IsVisibleUnit<T>(T unit) where T : UnitBase { bool result = ViewUtility.IsVisibleUnit(unit, CurrentEyes, visibleAngle, visibleDistance, visibleMask); // SENSORS if (!result) { foreach (AISensorBase sensor in sensors) { if (sensor != null) { if (sensor.DetectTarget<T>(unit)) { result = true; break; } } } } // END SENSORS return result; }
public class FragmentMeshCreator : MeshCreatorBase { public virtual void Create(float angle, float distance, float step = 10f) { List<Vector3> vertices = new List<Vector3>(); List<int> triangles = new List<int>(); List<Vector2> uvs = new List<Vector2>(); Vector3 right = ViewUtility.GetRotation(Vector3.forward, angle) * distance; Vector3 left = ViewUtility.GetRotation(Vector3.forward, angle) * distance; Vector3 from = left; vertices.Add(Vector3.zero); vertices.Add(from); uvs.Add(Vector2.one * 0.5f); uvs.Add(Vector2.one); int triangleIdx = 3; for (float angleStep = -angle; angleStep < angle; angleStep += step) { Vector3 to = ViewUtility.GetRotation(Vector3.forward, angleStep) * distance; // from = to; vertices.Add(from); uvs.Add(Vector2.one); triangles.Add(triangleIdx - 1); triangles.Add(triangleIdx); triangles.Add(0); triangleIdx++; } vertices.Add(right); uvs.Add(Vector2.one); Mesh mesh = new Mesh(); mesh.name = "FragmentArea"; mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uvs.ToArray(); mesh.RecalculateNormals(); myMeshFilter.mesh = mesh; } } // public static Vector3 GetRotation(Vector3 forward, float angle) { float rad = angle * Mathf.Deg2Rad; Vector3 result = new Vector3(forward.x * Mathf.Cos(rad) + forward.z * Mathf.Sin(rad), 0, forward.z * Mathf.Cos(rad) - forward.x * Mathf.Sin(rad)); return result; }
Source: https://habr.com/ru/post/256273/
All Articles