我想将一个演员移动到放置在世界上的一组演员。这组actors和移动actors为它们设置了碰撞。基本上,每当移动的actor到达其目的地之一时,重叠的actor就会在移动对象的类上设置一个属性,该属性是它下一个移动到的actor。
以下是重要的代码片段
这是应该更新的属性:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
ARAStaticMeshActor* CurrentTarget;
移动演员的 Tick。
void ARAScrapPart::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (CurrentTarget != nullptr)
{
ARAGameState* gameState = GetWorld()->GetGameState<ARAGameState>();
FVector targetLocation = CurrentTarget->GetActorLocation();
FVector currentLocation = GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("this %X CurrentTarget %X"), this, CurrentTarget);
if (FGenericPlatformMath::Abs(currentLocation.X - targetLocation.X) > 5.f
|| FGenericPlatformMath::Abs(currentLocation.Y - targetLocation.Y) > 5.f)
{
if (targetLocation.X > currentLocation.X)
{
currentLocation.X += Speed * DeltaTime;
}
if (targetLocation.X < currentLocation.X)
{
currentLocation.X -= Speed * DeltaTime;
}
if (targetLocation.Y > currentLocation.Y)
{
currentLocation.Y += Speed * DeltaTime;
}
if (targetLocation.Y < currentLocation.Y)
{
currentLocation.Y -= Speed * DeltaTime;
}
SetActorLocation(currentLocation);
}
}
}
而最重要的部分,重叠事件:
void ARAStaticMeshActor::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
ARAScrapPart* scrapPart = Cast<ARAScrapPart>(OtherActor);
if (scrapPart != nullptr)
{
ARAGameState* gameState = GetWorld()->GetGameState<ARAGameState>();
for (size_t i = 0; i < gameState->conveyorFields.Num(); i++)
{
if (gameState->conveyorFields[i]->Actor == this)
{
if (i != gameState->conveyorFields.Num() - 1)
{
UE_LOG(LogTemp, Warning, TEXT("OtherActor %X , ScrapPart %X , CurrentTarget %X , nextField %X"), OtherActor, scrapPart, scrapPart->CurrentTarget, gameState->conveyorFields[i + 1]->Actor);
scrapPart->CurrentTarget = gameState->conveyorFields[i + 1]->Actor;
UE_LOG(LogTemp, Warning, TEXT("After update CurrentTarget %X"), scrapPart->CurrentTarget);
}
else
{
scrapPart->Destroy();
}
break;
}
}
}
}
如您所见,我更新了 scrapPart->CurrentTarget = gameState->conveyorFields[i + 1]->Actor; 移动演员的目标,但它并没有在类本身上得到更新。
我已经通过 UE_LOGs 确认我在这里尝试修改的 scrapPart(移动部分)与 Ticks 相同。(检查 scrapPart 的地址和 Tick 函数中的 this 是一样的。)另外一个非常有趣的行为是,在修改 CurrentTarget 属性后的重叠函数范围内,UE_LOG 显示了一个更新的值,但是该值在scrapPart 的tick 函数上保持不变。
这是虚幻引擎5。
有没有人遇到过这样的事情?
编辑:添加了日志记录部分,这也是日志的输出:
LogTemp: Warning: OtherActor 3587A80 , ScrapPart 3587A80 , CurrentTarget 0 , nextField 3585080
LogTemp: Warning: After update CurrentTarget 3585080
LogTemp: Warning: OtherActor 3587A80 , ScrapPart 3587A80 , CurrentTarget 3585080 , nextField 3585080
LogTemp: Warning: After update CurrentTarget 3585080
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00
LogTemp: Warning: this 3587A80 CurrentTarget F0E5BD00