美文网首页
playercontroller自动寻路

playercontroller自动寻路

作者: 我真的不知道该起什么名字了 | 来源:发表于2019-05-06 16:02 被阅读0次

    如果 APlayerController需要自动寻路, 那么就要仿照AAIController, 添加组件:UPathFollowingComponent

        PathFollowingComponent = CreateDefaultSubobject<UPathFollowingComponent>(TEXT("PathFollowingComponent"));
        PathFollowingComponent->OnRequestFinished.AddUObject(this, &AAIController::OnMoveCompleted);
    
    void UAIBlueprintHelperLibrary::SimpleMoveToLocation(AController* Controller, const FVector& GoalLocation)
    {
        UNavigationSystemV1* NavSys = Controller ? FNavigationSystem::GetCurrent<UNavigationSystemV1>(Controller->GetWorld()) : nullptr;
        if (NavSys == nullptr || Controller == nullptr || Controller->GetPawn() == nullptr)
        {
            UE_LOG(LogNavigation, Warning, TEXT("UNavigationSystemV1::SimpleMoveToActor called for NavSys:%s Controller:%s controlling Pawn:%s (if any of these is None then there's your problem"),
                *GetNameSafe(NavSys), *GetNameSafe(Controller), Controller ? *GetNameSafe(Controller->GetPawn()) : TEXT("NULL"));
            return;
        }
    
        UPathFollowingComponent* PFollowComp = InitNavigationControl(*Controller);
    
        if (PFollowComp == nullptr)
        {
            FMessageLog("PIE").Warning(FText::Format(
                LOCTEXT("SimpleMoveErrorNoComp", "SimpleMove failed for {0}: missing components"),
                FText::FromName(Controller->GetFName())
            ));
            return;
        }
    
        if (!PFollowComp->IsPathFollowingAllowed())
        {
            FMessageLog("PIE").Warning(FText::Format(
                LOCTEXT("SimpleMoveErrorMovement", "SimpleMove failed for {0}: movement not allowed"),
                FText::FromName(Controller->GetFName())
            ));
            return;
        }
    
        const bool bAlreadyAtGoal = PFollowComp->HasReached(GoalLocation, EPathFollowingReachMode::OverlapAgent);
    
        // script source, keep only one move request at time
        if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
        {
            PFollowComp->AbortMove(*NavSys, FPathFollowingResultFlags::ForcedScript | FPathFollowingResultFlags::NewRequest
                , FAIRequestID::AnyRequest, bAlreadyAtGoal ? EPathFollowingVelocityMode::Reset : EPathFollowingVelocityMode::Keep);
        }
    
        // script source, keep only one move request at time
        if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
        {
            PFollowComp->AbortMove(*NavSys, FPathFollowingResultFlags::ForcedScript | FPathFollowingResultFlags::NewRequest);
        }
    
        if (bAlreadyAtGoal)
        {
            PFollowComp->RequestMoveWithImmediateFinish(EPathFollowingResult::Success);
        }
        else
        {
            const ANavigationData* NavData = NavSys->GetNavDataForProps(Controller->GetNavAgentPropertiesRef());
            if (NavData)
            {
                FPathFindingQuery Query(Controller, *NavData, Controller->GetNavAgentLocation(), GoalLocation);
                FPathFindingResult Result = NavSys->FindPathSync(Query);
                if (Result.IsSuccessful())
                {
                    PFollowComp->RequestMove(FAIMoveRequest(GoalLocation), Result.Path);
                }
                else if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
                {
                    PFollowComp->RequestMoveWithImmediateFinish(EPathFollowingResult::Invalid);
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:playercontroller自动寻路

          本文链接:https://www.haomeiwen.com/subject/zthdoqtx.html