When is this code executing? A root component on an actor doesn’t have a default or initial position/rotation since that is determined entirely by where the actor is spawned. If this code is in the constructor then it won’t do anything, but if you are running this code after the actor has already been spawned then I would expect any of your approaches to work (although you should probably be using a FRotator instead of FQuat, as -180 is not a valid value for a quaternion).
Treat the root component as if it has no transform in the constructor (because it doesn’t, anything you set there will be ignored when the actor is spawned).
You need to call SetWorldLocationAndRotation on the StaticMeshComponent, not the RootComponent.
Spawned
Or you can call this code right after spawning the Static Mesh Actor, if you are not using your own Static Mesh Actor class (for other readers mainly).
BP Defaults
You can also set this in your BP, in default properties, if you are using a BP of a static mesh actor.
So in summary:
Set Mobility to Movable!
Let us know how it goes!
Rama
PS: Static mesh actors default to static mobility for optimization reasons, allows lighting system of UE4 to do stuff it can’t do with moveable meshes.
#include "BasePlatform.h"
ABasePlatform::ABasePlatform(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
InitializeComponents(PCIP);
SetupMeshes();
SetupOrientation();
}
/*
Instantiates objects used
*/
void ABasePlatform::InitializeComponents(const class FPostConstructInitializeProperties& PCIP)
{
StaticMeshComponent = PCIP.CreateDefaultSubobject <UStaticMeshComponent>(this, TEXT("mySubObject"));
SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
}
/*
Set up all the mesh related stuff
*/
void ABasePlatform::SetupMeshes()
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> staticMesh(TEXT("StaticMesh'/Engine/EditorMeshes/EditorPlane'"));
StaticMeshObject = staticMesh.Object;
RootComponent = SceneComponent;
StaticMeshComponent->AttachTo(RootComponent);
StaticMeshComponent->SetStaticMesh(StaticMeshObject);
}
/*
Sets initial values for the platform position
*/
void ABasePlatform::SetupOrientation()
{
FVector Position;
Position.X = 0.0f;
Position.Y = 0.0f;
Position.Z = 0.0f;
FRotator Rotation;
Rotation.Yaw = -180.0f;
Rotation.Pitch = 0.0f;
Rotation.Roll = 0.0f;
StaticMeshComponent->SetMobility(EComponentMobility::Movable);
StaticMeshComponent->SetWorldLocationAndRotation(Position, Rotation);
}
My aim is to make a blueprint that starts off rotated in the correct orientation… Just to save me time (4 hours later… DOH)! But it will be the basis of all of my platforms considering it’s a side scroller
Currently when I drag my blue print into the scene, it’s facing the wrong way by default.