I wrote a BTTaskNode
that worked correctly with one character in my scene. However, after I added another character of the same class to my scene, everything broke. One character just stands still and does nothing, while the other character goes in a different direction than I indicated. I tried logging the start and target character positions, and they are calculated correctly. After that, I tried to rewrite my node, and instead of moving, it simply teleported by using SetActorLocation
. However, after this, both of my characters teleported to the same place and much further than I indicated. I tried asking ChatGPT and searching for a solution in forums, but I couldn’t find a case similar to mine.
Original code:
// Fill out your copyright notice in the Description page of Project Settings.
include “BTTask_MoveToWithoutRotation.h”
include “Math/UnrealMathUtility.h”
include “BehaviorTree/BlackboardComponent.h”
include “AIController.h”
UBTTask_MoveToWithoutRotation::UBTTask_MoveToWithoutRotation()
{
NodeName = “Move to target without changing rotation”;
bNotifyTick = true;
}
EBTNodeResult::Type UBTTask_MoveToWithoutRotation::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
Super::ExecuteTask(OwnerComp, NodeMemory);
OwnerAIController = OwnerComp.GetAIOwner();
OwnerPawn = OwnerAIController->GetPawn();
if (OwnerPawn == nullptr || OwnerAIController == nullptr)
{
return EBTNodeResult::Failed;
}
return EBTNodeResult::InProgress;
}
void UBTTask_MoveToWithoutRotation::TickTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickTask(OwnerComp, NodeMemory, DeltaSeconds);
FVector CurrentLocation = OwnerPawn->GetActorLocation();
FVector TargetLocation = OwnerComp.GetBlackboardComponent()->GetValueAsVector(GetSelectedBlackboardKey());
FVector ToSetLocation = FMath::VInterpConstantTo(CurrentLocation,
TargetLocation,
DeltaSeconds,
MovingSpeed
);
if (FVector::Dist(CurrentLocation, TargetLocation) <= 1)
{
FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
}
OwnerPawn->SetActorLocation(ToSetLocation);
}