【问题标题】:Atached Actor Mesh dont stick to Parent Actor附加的 Actor Mesh 不会粘在父 Actor 上
【发布时间】:2019-07-31 13:30:39
【问题描述】:

上下文:

我正在使用 C++ 在 ActorComponent (ShieldComponent) 中创建一些 Actors (Shields),这些 ActorComponent (ShieldComponent) 附加到 Actor (Ship) 上。 当我移动我的飞船时,演员(盾牌)会留在世界上的位置(不要随飞船移动)。

什么可能很重要:我没有在构造函数中调用CreateShieldActor,所以我不能使用ConstructorHelpers::FObjectFinder 代替StaticLoadObject/LoadObject 来加载Mesh/Material。

我的 C++ 创建代码如下所示:

AShieldPart* UShieldComponent::CreateShieldActor(AActor* Owner, FString MeshName)
{
    //Create Shield Actor
    FVector Location = Owner->GetActorLocation();
    FRotator Rotator(0, 0, 0);
    FVector Scale(10, 10, 10);
    FActorSpawnParameters SpawnInfo;
    SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
    SpawnInfo.Owner = Owner;

    FTransform Transform(Rotator, Location, Scale);
    UClass * MeshClass = AShieldPart::StaticClass();

    AShieldPart* ShieldPart = GetWorld()->SpawnActor<AShieldPart>(MeshClass, Transform, SpawnInfo);

    //Attach Mesh to Shield Actor
    ShieldPart->AttachToActor(Owner, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));

    //Set Mesh for Shield
    ShieldPart->SetNewMesh(MeshName);

    //Set Material
    UMaterial* Material = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT("/some/path")));
    ShieldPart->MeshComponent->SetMaterial(0, Material);

    return ShieldPart;
}


AShieldPart::AShieldPart()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    SetRootComponent(RootComponent);

    MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
    MeshComponent->SetupAttachment(RootComponent);
}

void AShieldPart::SetNewMesh(FString MeshName)
{
    UStaticMesh* Mesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *MeshName));
    //UStaticMesh* Mesh = LoadObject<UStaticMesh>(nullptr, *MeshName);
    MeshComponent->SetStaticMesh(Mesh);
    MeshComponent->SetCollisionProfileName(TEXT("BlockAll"));
    MeshComponent->SetNotifyRigidBodyCollision(true);
    MeshComponent->SetSimulatePhysics(true);
    MeshComponent->SetEnableGravity(false);

    MeshComponent->RegisterComponent();
}

这会更有趣,当我使用 BP 创建那个演员(盾牌)时,一切正常......

【问题讨论】:

    标签: c++ visual-studio unreal-engine4


    【解决方案1】:

    结果证明解决方案是将USceneComponent 删除为RootElement。主元素必须是UStaticMeshComponent,然后才能附加到父元素(Ship)。

    AShieldPart::AShieldPart()
    {
        // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
        PrimaryActorTick.bCanEverTick = true;
    
        // Creating MeshComponent and attach to RootComponent
        MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
        RootComponent = MeshComponent;
        MeshComponent->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
    
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2017-03-10
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多