c++unreal-engine4unreal-engine5unreal-blueprint

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;

Solution

  • 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->Mobility = EComponentMobility::Movable;
    2. 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.