Example #1
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;
    }