Cannot move C++ actor (with root component)

I feel I am just doing something really stupid, but I cannot make my custom actor movable in the editor or in the game.

The actor directly inherits from AActor.

I have tried using a UStaticMeshComponent as RootComponent as well as a USceneComponent with the mesh being a child of the scene (as I found some posts suggesting that a USceneComponent would be required). All of the components are stured as EditAnywhere and BlueprintReadWrite properties of the actor:

this->SceneRoot = this->CreateDefaultSubobject<USceneComponent>(TEXT("Scene Root"));
this->SceneRoot->Mobility = EComponentMobility::Type::Movable;

this->CoordinateSystemMesh = this->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Coordinate System"));
this->CoordinateSystemMesh->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
this->CoordinateSystemMesh->Mobility = EComponentMobility::Type::Movable;
this->CoordinateSystemMesh->SetGenerateOverlapEvents(false);
this->CoordinateSystemMesh->SetSimulatePhysics(false);
this->CoordinateSystemMesh->SetStaticMesh(Mesh);
this->CoordinateSystemMesh->SetRelativeScale3D(FVector(1.0f / 50.0f));
this->CoordinateSystemMesh->AttachToComponent(this->SceneRoot, FAttachmentTransformRules::KeepRelativeTransform, TEXT("Coordinate System"));

this->SetRootComponent(this->SceneRoot);

The problem is that (in both cases)

  • If I try to change the position of the actor at runtime (i.e. outside the constructor in methods like BeginPlay) using SetActorLocation or SetActorLocationAndRotation, the methods have no effect and return false.
  • In the editor, my actor shows the transform gizmo, but if I move it somewhere else, it snaps back once I release the mouse button.
  • In the editor, if the actor itself is selected, the transform does not show up. I need to select the root component to see the transform. For all other actors (e.g. StaticMeshActor), selecting the actor itself shows the transform of the root component in the details view, so I guess this is a symptom of the problem.

What do I need to do to fix that? My main goal is that SetActorLocationAndRotation works at runtime such that the actor itself can be moved regardless of the root component that is set (provided one is set).

Thanks in advance,
Christoph

1 Like

What do you mean by “using OnConstruction”? I am C++ -only atm and I did not override the method.

I also think the problem is rather caused by something I am not doing than by something I am doing, most likely something in the UPROPERTY annotations. One weird thing that I noticed: While I cannot see the transform itself when selecting the actor in the editor, I see the Mobility part of it, which makes me believe that the problem is the transform being somehow locked. However, I have no clue what could cause that (I am using UE only for a week now …).

Hello,

I’ve just copy pasted your code and put it in construction code as is, it works as expected and everything movable. Are you sure you put in constructor ie AMyActor::AMyActor?

Also, a couple of tips:

  • You should make your components VisibleAnywhere instead of EditAnywhere if you’re not planning on replacing components itself. Basically EditAnywhere allow you to replace USceneComponent with something else, while VisibleAnywhere just let you access properties, which you’re trying to do there I guess
  • I recommend setting SceneRoot as RootComponent after spawning the component itself, before you attach anything to the component
  • AttachToComponent is used for runtime attachment. While it’s still possible to use during initialization, try using SetupAttachment instead
  • You don’t have to use this pointer

But honestly, I think something weird happened during hotreloading, try to create a new C++ class, copypaste this code and then create new BP from it

Thanks, that was very insightful, specifically the SetupAttachment thing.

I have tried all your suggestions, but to no avail, so I resorted to the last one and recreated the actor and copied the code. Now it’s working. I have probably screwed up something when trying a lot of different things from the Internetz to fix the issue.