StaticMeshComponent在UnrealEngine中为什么不能移动?

huangapple go评论126阅读模式
英文:

Why may StaticMeshComponent not move around in UnrealEngine?

问题

I use the Character C++ class to move the character in my game via AddInputMovement().

However, it only moves if there's no MeshComponent attached to it. If I attach StaticMeshComponent, it stops reacting to any keys and moving, while if I attach a common MeshComponent, it requires a Skeletal mesh in Blueprint Editor, although my spaceship .fbx model is not one.

What should I do? What are the reasons a StaticMesh might refuse to move?

ASpaceship::ASpaceship()
{
    PrimaryActorTick.bCanEverTick = true;

    // !!! These lines prevent the character from moving for some reason...
    MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
    RootComponent = MeshComponent;
}

请注意,代码部分已被保留,没有进行翻译。

英文:

I use the Character C++ class to move the character in my game via AddInputMovement().

However, it only moves if there's no MeshComponent attached to it. If I attach StaticMeshComponent, it stops reacting to any keys and moving, while if I attach a common MeshComponent, it requires a Skeletal mesh in Blueprint Editor, although my spaceship .fbx model is not one.

What should I do? What are the reasons a StaticMesh might refuse to move?

ASpaceship::ASpaceship()
{
	PrimaryActorTick.bCanEverTick = true;

    // !!! These lines prevent the character from moving for some reason...
	MeshComponent = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT(&quot;MeshComponent&quot;));
	RootComponent = MeshComponent;

答案1

得分: 0

Static Mesh Components (UStaticMeshComponent),就像从USceneComponent继承的任何其他组件一样,如果它们的Mobility属性设置为"Moveable"(EComponentMobility::Type::Moveable),它们可以移动。

为确保您添加到角色的网格组件将与角色一起移动,您需要确保以下几点:

  1. 它被设置为可移动:YourMeshComponent->Mobility = EComponentMobility::Movable;
  2. 它附加到角色的根组件:YourMeshComponent->AttachToComponent(YourActor->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);

注意:在代码中,将YourActor替换为您的角色,将YourMeshComponent替换为您的网格。

现在,对您的角色的任何变换更改都将应用于该网格。

英文:

Static Mesh Components (UStaticMeshComponent), just like any other component inheriting from USceneComponent, can and do move, if their Mobility property is set to "Moveable" (EComponentMobility::Type::Moveable).

To make sure the mesh component you added to the actor will move together with your actor, you need to make sure of several things:

  1. It is set to moveable: YourMeshComponent-&gt;Mobility = EComponentMobility::Movable;
  2. It is attached to the actor's root component: YourMeshComponent-&gt;AttachToComponent(YourActor-&gt;GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);.

Note: in the code, replace YourActor is your actor and YourMeshComponent is your mesh.

Now, any changes to your actor's transform will be applied to the mesh.

huangapple
  • 本文由 发表于 2023年3月31日 22:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定