/// <summary> /// Get the most threatening dynamic obstacle in range /// </summary> /// <returns>The most threatening dynamic obstacle in range</returns> private SteeringExternalElement_Dynamic GetMostThreateningDynamicObstacle() { List <SteeringExternalElement_Dynamic> dynamicObstacleList = SteeringExternalElement_Dynamic.ElementList; List <SteeringExternalElement_Dynamic> threateningObstacleList = new List <SteeringExternalElement_Dynamic>(); Vector3 obstacleFuturePosition = Vector3.zero; Vector3 futurePosition = Vector3.zero; SteeringExternalElement_Dynamic dynamicObstacle = null; float sumRadius = 0; // Get all threatening obstacle for (int i = 0; i < dynamicObstacleList.Count; i++) { dynamicObstacle = dynamicObstacleList[i]; if (dynamicObstacle != null) { if (dynamicObstacle.gameObject != gameObject) { if ((dynamicObstacle.transform.position - transform.position).sqrMagnitude < m_ObstacleMaxDistance * m_ObstacleMaxDistance) { obstacleFuturePosition = dynamicObstacle.transform.position + dynamicObstacle.Velocity; futurePosition = transform.position + SteeringCore.Velocity; sumRadius = m_BoundingSphereRadius + dynamicObstacle.BoundingSphereRadius; if ((obstacleFuturePosition - futurePosition).sqrMagnitude < sumRadius * sumRadius) { threateningObstacleList.Add(dynamicObstacle); } } } } } if (threateningObstacleList.Count <= 0) { return(null); } float nearestSqrDist = 0; float sqrDist = 0; SteeringExternalElement_Dynamic nearestDynamicObstacle = threateningObstacleList[0]; // Get nearest threatening obstacle for (int i = 0; i < threateningObstacleList.Count; i++) { dynamicObstacle = threateningObstacleList[i]; nearestSqrDist = (transform.position - nearestDynamicObstacle.transform.position).sqrMagnitude; sqrDist = (transform.position - dynamicObstacle.transform.position).sqrMagnitude; if (sqrDist < nearestSqrDist) { nearestDynamicObstacle = dynamicObstacle; } } return(nearestDynamicObstacle); }
/// <summary> /// Unaligned obstacle avoidance behavior /// </summary> public override void PerformSteeringBehavior() { if (SteeringCore == null) { return; } Vector3 avoidanceForce = Vector3.zero; // Get most threatening obstacle m_DynamicObstacle = GetMostThreateningDynamicObstacle(); // Calculate avoidance force if (m_DynamicObstacle != null) { avoidanceForce = transform.position + SteeringCore.Velocity - m_DynamicObstacle.transform.position; if (Vector3.Dot(avoidanceForce, SteeringCore.Velocity) < -0.9f) { avoidanceForce = transform.right; } } if (avoidanceForce != Vector3.zero) { // Calculate desired velocity m_DesiredVelocity = (avoidanceForce).normalized * SteeringCore.MaxSpeed; // Calculate steering force SteeringForce = m_DesiredVelocity - SteeringCore.Velocity; } else { SteeringForce *= m_SteeringForceConservation; } }