コード例 #1
0
    public float GetDistanceByMapPoint(Vector2 point, out bool isOutOfPath, out Vector2 segmentNomal)
    {
        float   pathDistance = 0, segmentLengthTotal = 0;
        float   minDistSqr = float.MaxValue;
        Vector2 mapPoint;

        segmentNomal = Vector2.zero;
        for (int i = 1; i < m_path.Count; i++)
        {
            float projectionLength = 0;
            float distSqr          = SteerManager.GetPointToSegmentDistanceSqr(point, m_path[i - 1], m_path[i], m_normals[i], m_lengths[i], out mapPoint, out projectionLength);
            if (distSqr < minDistSqr)
            {
                minDistSqr   = distSqr;
                segmentNomal = m_normals[i];
                pathDistance = segmentLengthTotal + projectionLength;
            }
            segmentLengthTotal += m_lengths[i];
        }
        isOutOfPath = minDistSqr > m_radiusSqr;
        return(pathDistance);
    }
コード例 #2
0
    public void LogicUpdate()
    {
        Obstacle collider;
        Vector2  obstacleAvoidSteer = SteerManager.GetObstacleAvoidSteer(this, SteerManager.Instance.Obstacles, out collider);

        if (collider != null && Random.Range(0, 100) > SteerManager.Instance.IgnoreCollisionProbability)
        {
            float hardRadiusSum = collider.HardRadius + Radius;
            float distanceSqr   = (Position - collider.Position).sqrMagnitude;
            if (distanceSqr <= hardRadiusSum * hardRadiusSum)
            {
                Forward   = (Forward + obstacleAvoidSteer).normalized;
                Direction = (Forward + obstacleAvoidSteer).normalized;
            }
            else
            {
                float nextPositionDistanceSqr = (NextPosition - collider.Position).sqrMagnitude;
                if (nextPositionDistanceSqr <= hardRadiusSum * hardRadiusSum)
                {
                    Forward = (Forward + obstacleAvoidSteer).normalized;
                    stuckFrames++;
                    if (stuckFrames > 2)
                    {
                        Forward   = SteerManager.GetStuckSolveSteer(this, SteerManager.Instance.Obstacles);
                        Direction = Forward;
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
        else
        {
            Boid    collideNeighbor;
            Vector2 seprationSteer = SteerManager.GetSeprationSteer(this, Neighbors, out collideNeighbor);
            if (collideNeighbor != null && Random.Range(0, 100) > SteerManager.Instance.IgnoreCollisionProbability)
            {
                Direction = (Forward + seprationSteer).normalized;
            }
            else
            {
                seprationSteer *= SteerManager.Instance.SeprationWeight;
                Vector2 pathfollowSteer = SteerManager.GetPathFollowSteer(this, Path) * SteerManager.Instance.PathFollowWeight;
                Vector2 alignmentSteer  = SteerManager.GetAlignmentSteer(this) * SteerManager.Instance.AlignmentWeight;
                Vector2 cohesionSteer   = SteerManager.GetCohesionSteer(this) * SteerManager.Instance.CohesionWeight;
                if (SteerManager.Instance.PathFollowWeight != 0)
                {
                    Direction = (Forward + pathfollowSteer + seprationSteer).normalized;
                }
                else
                {
                    Direction = (Forward + pathfollowSteer + seprationSteer + alignmentSteer + cohesionSteer).normalized;
                }
            }
        }
        Forward     = Direction;
        Position    = NextPosition;
        stuckFrames = 0;
    }
コード例 #3
0
 private void Awake()
 {
     Instance = this;
 }