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