void ATankAIController::Tick(float deltaTime) { Super::Tick(deltaTime); ATank* PlayerTank = GetPlayerTank(); if (PlayerTank) { GetControlledTank()->AimAt(PlayerTank->GetActorLocation()); } }
void ATankAIController::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); ATank* PlayerTank = Cast<ATank>(GetWorld()->GetFirstPlayerController()->GetPawn()); auto ControlledTank = Cast<ATank>(GetPawn()); if (PlayerTank) { ControlledTank->AimAt(PlayerTank->GetActorLocation()); } ControlledTank->Fire(); }
void ABotController::SetEnemy(class APawn* InPawn) { //Set the blackboard keys to the object and location of the found enemy, for use in the MoveTo behavior tree task BlackboardComp->SetValue<UBlackboardKeyType_Object>(EnemyKeyID, InPawn); BlackboardComp->SetValue<UBlackboardKeyType_Vector>(EnemyLocationID, InPawn->GetActorLocation()); //Calculates and sets the direction for the AI bot's projectile ABot* ThisBot = Cast<ABot>(GetPawn()); ATank *Enemy = Cast<ATank>(this->BestPawn); FVector dir; dir = Enemy->GetActorLocation() - ThisBot->GetActorLocation(); //Finds the vector between the AI bot and the player tank dir = dir.GetSafeNormal(1.0f) * 1000; //Normalizes and scales the vector for a uniform speed ThisBot->Direction = FVector(dir.X, dir.Y, 0.f); //Zeros out the Z value so the projectile moves solely on the horizontal plane }
void ATankAIController::BeginPlay() { Super::BeginPlay(); ATank* tank = GetPlayerTank(); if (!tank) { UE_LOG(LogTemp, Warning, TEXT("No controlled tank for this controller")); } else { FString name = tank->GetName(); UE_LOG(LogTemp, Warning, TEXT("We are now controlling tank: %s"), *name); } }
// Function to set the bot's target to the closest player-controlled tank void ABotController::SearchForEnemy() { APawn* MyBot = GetPawn(); ABot* Tank = Cast<ABot>(GetPawn()); if (MyBot == NULL) return; //indicate the that the game has now started BlackboardComp->SetValue<UBlackboardKeyType_Bool>(HasStartedID, true); const FVector MyLoc = MyBot->GetActorLocation(); float BestDistSq = MAX_FLT; //Iterate through the world objects, and set the closest found player to BestPawn for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; It++) { ATank* TestPawn = Cast<ATank>(*It); if (TestPawn) { const float DistSq = FVector::Dist(TestPawn->GetActorLocation(), MyLoc); if (DistSq < BestDistSq) { BestDistSq = DistSq; BestPawn = TestPawn; if (PawnCanBeSeen(*It)){ FRotator Rot = FRotationMatrix::MakeFromX(BestPawn->GetActorLocation() - Tank->GetActorLocation()).Rotator(); //Find the rotator required for the turret to face it's target Tank->BotTurret->SetWorldRotation(FRotator(0.f, Rot.Yaw, 0.f)); //Point the turret to it's target } } } } if (BestPawn) { SetEnemy(BestPawn); //Once the closest player is found, set the Bot's target } }
void ATankAIController::BeginPlay() { Super::BeginPlay(); ATank* ThisTank = GetControlledTank(); if (!ThisTank) { GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Red, TEXT("[TAIC] AI Tank not found")); } ATank* PlayerTank = GetPlayerTank(); if (PlayerTank) { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("[TAIC] Player Tank %s found!"), *PlayerTank->GetName())); } else { GEngine->AddOnScreenDebugMessage(-1, 20.f, FColor::Red, TEXT("[TAIC] Player Tank not found")); } }