Hello, I don’t understand why AttachToComponent() is not working.
I have basic VR Pawn like so:
What I want to do at the BeginPlay() function is to spawn and attach some cards to my PlayerDeck Component.
Here is my code in my Pawn class BeginPlay function:
void AMyPlayer::BeginPlay()
{
Super::BeginPlay();
stateDeck = DECK_CLOSE;
FVector loc = LeftHandMesh->GetComponentLocation();
FRotator rot = LeftHandMesh->GetComponentRotation();
for (int i = 0; i < 8; i++)
{
ACard* newCard = dynamic_cast<ACard*>(this->GetWorld()->SpawnActor(ACard::StaticClass(), &loc, &rot));
playerCards.Add(newCard);
}
int i = 0;
for (ACard* elem : playerCards)
{
elem->getStaticMeshComp()->SetRelativeTransform(positionCardPlayer[i]);
elem->getStaticMeshComp()->AttachToComponent(PlayerDeck, FAttachmentTransformRules::KeepRelativeTransform);
elem->getStaticMeshComp()->SetSimulatePhysics(false);
i++;
}
}
My ACard class inherit from an ACollectible class who inherit from AActor.
I really don’t understand why it is not working because on my Grib function I made I use AttachToComponent and it’s working… as you can see here:
void AMyPlayer::gripObject()
{
AActor* cActor = NULL; //here to find the closet object if their is one overlapping
TArray<AActor*> overlapping;
RightControllerCollision->GetOverlappingActors(overlapping);
for (AActor* elem : overlapping)
{
if (cActor == NULL && elem->GetClass()->IsChildOf(ACollectible::StaticClass()))
{
cActor = elem;
}
}
if (cActor != NULL)
{
ACollectible* gActor = dynamic_cast<ACollectible*>(cActor);
gActor->getStaticMeshComp()->AttachToComponent(RightController, FAttachmentTransformRules::KeepWorldTransform);
gActor->getStaticMeshComp()->SetSimulatePhysics(false);
}
}
I tried changing the AttachementTransformRules but whithout results. I check if the function is called and it is.
I also tried to spawn one card and attach it to the player deck directly in a function which need to be trigger by a button and it’s not working either…
If anyone have an idee or know why it is not working I will be grateful.
Thanks