private List <VisibleObjectData> FindVisibleObjects() { List <VisibleObjectData> visibleObjects = new List <VisibleObjectData>(); Vector3 currentPosition = this.transform.position; Collider[] colliders = Physics.OverlapSphere(currentPosition, RADIUS); foreach (Collider collider in colliders) { Transform objectFound = collider.transform; Vector3 direction = (objectFound.position - currentPosition).normalized; float distance = Vector3.Distance(currentPosition, objectFound.position); RaycastHit hit; if (IsInFieldOfView(direction) && Physics.Raycast(currentPosition, direction, out hit, distance)) { // TODO - Fix detection. For now, do not consider other helicopters or objects which are under specified height GameObject hittedObject = hit.transform.gameObject; if (hittedObject.tag != "helicopter") { VisibleObjectData data = createDataFrom(hit); visibleObjects.Add(data); } } } return(visibleObjects); }
public Vector3 Avoid(Rigidbody body, VisibleObjectData data) { Vector3 offset = body.position - data.position; Vector3 direction = Vector3.ProjectOnPlane(offset, body.transform.forward); return(direction.normalized); }
private void Avoid(VisibleObjectData closestObject) { Vector3 steering = steeringBehaviors.Avoid(body, closestObject) * AVOIDANCE_VELOCITY; movement.ReduceSpeed(body); body.AddForce(steering); }
private bool IsAvoiding() { VisibleObjectData visibleObject = sensor.FindClosestObject(body.transform.position); bool isAvoiding = (visibleObject != null); if (isAvoiding) { Avoid(visibleObject); } return(isAvoiding); }
public VisibleObjectData FindClosestObject(Vector3 currentPosition) { VisibleObjectData closestObject = null; float closestDistance = float.PositiveInfinity; List <VisibleObjectData> visibleObjects = FindVisibleObjects(); foreach (VisibleObjectData data in visibleObjects) { float distance = Vector3.Distance(currentPosition, data.position); if (closestDistance >= distance) { closestObject = data; } } return(closestObject); }