Hello,
I’m somewhat new to UE4 and could use some help to better understand some functionality.
For context, I’m trying to create a component that simply spawns a mesh next to its parent actor. I would also like it to follow the actor but maintain it’s own relative position.
What brings me here is that I can’t quite figure out how to grab the relative position of the parent actor. What seems to be happening is that I’m grabbing some default or uninitialized location (so (0.0f, 0.0f, 0.0f). When I drag my pawn into the scene and release, my component mesh snaps to that centered location. This is shown below, where the gold sphere is my ActorComponent:
Here’s the code snippet from the constructor of my ActorComponent:
// OrbitActorComponent.cpp
...
// Get Parent Actor
AActor* ParentActor = GetOwner();
if (ParentActor)
{
UE_LOG(LogTemp, Warning, TEXT("The owner is valid!"));
}
// Get Parent Location
FVector ParentLocation;
if (ParentActor)
{
UE_LOG(LogTemp, Warning, TEXT("We have a location!"));
ParentLocation = ParentActor->GetActorLocation();
}
// Specify mesh for the rotating object
OurOrbitMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OrbitRepresentation"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
static ConstructorHelpers::FObjectFinder<UMaterial> SphereMaterial(TEXT("/Game/StarterContent/Materials/M_Metal_Gold.M_Metal_Gold"));
if (SphereVisualAsset.Succeeded())
{
OurOrbitMesh->SetStaticMesh(SphereVisualAsset.Object);
OurOrbitMesh->SetWorldLocation(FVector(ParentLocation.X + 10.f, ParentLocation.Y, ParentLocation.Z));
OurOrbitMesh->SetWorldScale3D(FVector(0.8f));
}
...
So GetActorLocation() is returning (0.0f, 0.0f, 0.0f) rather than (220.0f, 350.0f, 150.0f) which is where the pawn is located in the image above?
In my actor header I create an instance of the actor component:
// CollidingPawn.h
...
// Orbiting Component
UPROPERTY()
class UOrbitActorComponent* OurOrbitComponent;
...
And specify the subobject:
// CollidingPawn.cpp
...
// Instance of orbit component
OurOrbitComponent = CreateDefaultSubobject<UOrbitActorComponent>(TEXT("OrbitComponent"));
...
Additionally, when I click Play the component mesh actually moves with my pawn and maintains a fixed distance? If someone could explain that behaviour to me it would be incredibly helpful. Is it inheriting the input functionality from my pawn?
Thank you!