How to fix the RootComponent's Location?

Hi,

I am new to game development and Unreal Engine 4. I am using version 4.7

I have created a StaticMeshComponent and kept that as a RootComponent. Also I kept its Stability to Static.

Now I am trying to fix its location to (0, 0, 0) i.e. when I place that actor to the viewport, its location should be in (0, 0, 0).

.cpp file

AMyActor1::AMyActor1()

{

 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticComponent"));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> ChairMesh1(TEXT("Content'/Chairs_Chair1.uasset'"));

	RootComponent = StaticMeshComponent;
	FVector Location(0, 0, 0);
	StaticMeshComponent->Mobility = EComponentMobility::Static;
	StaticMeshComponent->SetStaticMesh(Chair1);


}

Can somebody please help me to find the solution for this issue.

Thanks in advance!

Hi ,

Static mesh component has zero offset from actor location by default. It looks like your actor instance has non-zero location.

Hope it helps!

Hi, Thank you so much for your reply. If I didnt add any code, I look the location is in zero offset. But after adding the code, it looks like the location is not in zero offset.

How to fix this issue?

What you mean not in zero offset?? Your actor or your root component. And viewport are you mean editor viewport or blueprint viewport?? If editor it’s your actor, but if in blueprint that is your root component that not in zero location

Sorry that if my explanation is wrong. As I have understood,
Zero offset sets the actor and component to the (0, 0, 0) location. After I create C++ Actor class and compile without adding any code to it, when I add that class to the editor viewport, I should add StaticMeshComponent and change its Mesh. After creating and setting the mesh, its location will be (0, 0, 0). Now I have added a StaticMeshComponent and kept as a RootComponent. Also I have added the Mesh in the code. But after compiling, I can see the Mesh is not changing it in Details panel. If I add this class to the editor Viewport, the location is in non-zero offset (its location is not in (0, 0, 0).

Tell me if i wrong interpretting your question. So you had an actor class. And when you wanted to put it in the editor viewport. You must first, create a blueprint isn’t it? And within that blueprint you set the root component to be staticMeshComponent (using add component) is it right?? After that you drag that blueprint (in the editor viewport) to the world. So it will create an actor from your blueprint, and you manually set the transform of that actor to 0,0,0. Is it right??

No. Actually, I need to add StaticMeshComponent and set its Mesh and Location to (0, 0, 0) in C++ code. Not in Blueprint. If I need to do this in Blueprint/C++, I do not want to change the Mesh and Location in Details Panel manually. Instead I need to do this automatically using code.

FVector Location(0, 0, 0); is not enough to set the actor location…

Just use SetActorLocation(FVector::ZeroVector);

It will be set to 0,0,0 once you play your game, not directly in the editor, since usually you want to be able to move it around. If you absolutely need that actor to be at 0,0,0 even when in the editor edition (which, to be frank, is weird) then you can overrirde the OnConstruction() method and do the SetActorLocation there.

Oh. So i think if it’s like that you can just set the componentlocation using SetComponentLocation or SetRelativeLocation to FVector::ZeroVector, but said it must be not an issue, because it’s like that by default.

I have tried with
StaticMeshComponent->SetRelativeLocation(FVector::ZeroVector);
and
SetActorLocation(FVector::ZeroVector);
but its not working.

I have tried with
StaticMeshComponent->SetRelativeLocation(FVector::ZeroVector);
and
SetActorLocation(FVector::ZeroVector);
but its not working.

For spawning actor from code:

GetWorld()->SpawnActor(AWarshipFleet::StaticClass(), FVector(0,0,0), SpawnRotation);

For set actor to zero in runtime try to use this:

StaticMeshComponent->Mobility = EComponentMobility::Movable;

void ATestActor::BeginPlay()
{
	Super::BeginPlay();
	SetActorLocation(FVector(0, 0, 0));
}

In addition, can you explain how you want to spawn actor in scene (setting in editor or with code)?

I have imported an fbx file: Chair. I want to set the location of that Chair using C++ code. Since I have 20 Chairs, its difficult to change each location to (0, 0, 0) manually. So I am trying to set the Mesh and Location in C++ code.

Have you recompiled your code? What is happening when you hit play? Have you tried putting the SetActorLocation() in BeginPlay() instead?

Yes, it worked when I added that in BeginPlay() method.

The above code. Should works.

The above code worked in BeginPlay() method. So this will work when the game begins but I am trying to change the location when I add that class to the editor viewport.

You can call that in spawned event like PostActorCreated or OnConstruction. See the initialization state ofan actor in ue docs for further information. You can also see the function in actor.h

Thank you all so much for the help. I have just created one Actor class in C++ but when I place it many times and change the Mesh, as you all said I dont think location will be a problem since it all places in correct order. Now I have to think to place the other objects within the same C++ Actor class code. Is it possible?

U simply need to create a dummy USceneComponent which will contain all your components. To do so first create a Scene :

RootScene = CreateDefaultSubobject<USceneComponent>(TEXT("DummyRootScene"));

Then attach components to this scene:

MyComponent->AttachTo(RootScene);

This will bring ur components to relative zero position with the RootScene.

Hope this helps…