模仿黑魂锁定目标功能

    if (bLocked)
    {
        //取消锁定状态
        bLockedTarget = false;
        bUseControllerRotationYaw = false;
        GetCharacterMovement()->bOrientRotationToMovement = true;
    }
    else
    {
        //锁定目标
        bLockedTarget = true;
        bUseControllerRotationYaw = true;
        GetCharacterMovement()->bOrientRotationToMovement = false;

        //计算最近的目标
        TArray<AActor*> TempActorArray;
        CharacterVisualField->GetOverlappingActors(TempActorArray);
        CharacterNearbyMonster.Reset();
        for (int i = 0; i < TempActorArray.Num();++i)
        {
            AMonster *TempActor=Cast<AMonster>(TempActorArray[i]);
            if (TempActor&&!TempActor->IsPendingKill())
            {
                CharacterNearbyMonster.Add(TempActor);
            }
        }

        if (CharacterNearbyMonster.Num()>0)
        {
            LockedTargetObject = CharacterNearbyMonster[0];
            for (int i = 1; i<CharacterNearbyMonster.Num(); ++i)
            {
                FVector temp = ((AMonster*)CharacterNearbyMonster[i])->GetActorLocation() - GetActorLocation();
                temp.Normalize();
                
                FVector CurrenLockedTargetVector = LockedTargetObject->GetActorLocation() - GetActorLocation();
                CurrenLockedTargetVector.Normalize();

                if (FVector::DotProduct(CurrenLockedTargetVector, FollowCamera->GetForwardVector()) < FVector::DotProduct(temp, FollowCamera->GetForwardVector()))
                {
                    LockedTargetObject = CharacterNearbyMonster[i];
                    
                }
                
            }
        }
    }

直接上代码,还有很多地方可以优化,仅供抛砖引玉,效果如图

 

posted @ 2016-06-28 21:14  湛蓝玫瑰  阅读(722)  评论(0编辑  收藏  举报