I have a c++ program where I am trying to rotate a static mesh that is attached to another static mesh.
My Pawn class derives from Default Pawn. When it drops to the floor, it works correctly, and if I rotate the
attached static mesh, it works correctly, but if I rotate the parent static mesh, the attached mesh rotates
into the floor as if the collision was ignored. See the code and screenshots below. Could this be related to
this bug:
Is there a workaround, or should I use sockets or physical constraint or something else?
Any help/advice would be appreciated.
thank you
new project c++, basic code
delete chairs, etc. scale floor way up in x,y
change USE_FEMUR to switch between rotating attached vs parent mesh
MyProject2GameModeBase.cpp
void AMyProject2GameModeBase::BeginPlay()
{
Super::BeginPlay();
ATestPawn* testPawn = GetWorld()->SpawnActor<ATestPawn>(ATestPawn::StaticClass(), FVector(1600, 1600, 400), FRotator(0, 0, 0), FActorSpawnParameters());
USphereComponent* testPawnCollision = testPawn->GetCollisionComponent();
testPawnCollision->SetSimulatePhysics(true);
testPawnCollision->SetEnableGravity(true);
}
TestPawn.cpp
#define USE_FEMUR true
int16 deltaDir = +10;
int16 minRotation = USE_FEMUR ? 90 : 30;
int16 maxRotation = USE_FEMUR ? 120 : 60;
UStaticMeshComponent* Leg1;
UStaticMeshComponent* Leg2;
UStaticMeshComponent* Leg3;
float LegRotation;
ATestPawn::ATestPawn()
{
PrimaryActorTick.bCanEverTick = true;
((USphereComponent*)RootComponent)->SetSphereRadius(5.0f,true);
const float Scale = GetCollisionComponent()->GetUnscaledSphereRadius() / 160.f;
GetMeshComponent()->SetRelativeScale3D(FVector(Scale));
Leg1 = AddLeg(RootComponent, 0);
Leg2 = AddLeg(RootComponent, 1);
Leg3 = AddLeg(RootComponent, 2);
LegRotation = minRotation;
}
UStaticMeshComponent* ATestPawn::AddLeg(USceneComponent* Sphere, int loop)
{
float angle = loop * 120.0f;
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(*FString("/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder"));
FName FemurName = *(FString("Femur_") + FString::FromInt(loop));
UStaticMeshComponent* Femur = CreateDefaultSubobject<UStaticMeshComponent>(FemurName);
Femur->SetStaticMesh(MeshAsset.Object);
Femur->SetupAttachment(Sphere);
Femur->SetRelativeRotation(FRotator(90.0f, angle, 0.0f));
Femur->SetWorldScale3D(FVector(1, 1, 4));
FName TibiaName = *(FString("Tibia_") + FString::FromInt(loop));
UStaticMeshComponent* Tibia = CreateDefaultSubobject<UStaticMeshComponent>(TibiaName);
Tibia->SetStaticMesh(MeshAsset.Object);
Tibia->SetupAttachment(Femur);
Tibia->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f));
Tibia->SetRelativeRotation(FRotator(30.0f, 0.0f, 0.0f));
Tibia->SetWorldScale3D(FVector(1,1,4));
return USE_FEMUR ? Femur : Tibia;
}
void ATestPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
LegRotation += deltaDir * DeltaTime;
Leg1->SetRelativeRotation(FRotator(LegRotation, 0.0f, 0.0f));
if (LegRotation <= minRotation) { deltaDir = +10; }
if (LegRotation >= maxRotation) { deltaDir = -10; }
}
no rotation:
rotate attached mesh:
rotate parent mesh: