英文:
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<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent = MeshComponent;
答案1
得分: 0
Static Mesh Components (UStaticMeshComponent
),就像从USceneComponent
继承的任何其他组件一样,如果它们的Mobility
属性设置为"Moveable"(EComponentMobility::Type::Moveable
),它们可以移动。
为确保您添加到角色的网格组件将与角色一起移动,您需要确保以下几点:
- 它被设置为可移动:
YourMeshComponent->Mobility = EComponentMobility::Movable;
- 它附加到角色的根组件:
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:
- It is set to moveable:
YourMeshComponent->Mobility = EComponentMobility::Movable;
- It is attached to the actor's root component:
YourMeshComponent->AttachToComponent(YourActor->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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论