Attached is a sample project which demonstrates the problem. The scene 2 StaticMeshActor actors: a cube and a sphere. The cube has an additional physics constraint component so that the sphere is constrained.
Input setup:
Virtual joystick X-axis: move the cube along the world Y-axis (AMyPawn::MoveX)
Virtual joystick Y-axis: change the linear Z limit of the physics constraint (distance between cube and sphere) (AMyPawn::MoveY)
The problem:
If the cube is moved first when the level starts, everything works properly. However, if only the joystick Y-axis is moved when the level starts, the sphere does move at all. Then if the cube is moved, the sphere moves suddenly according to the z-limit set previously. Can anyone tell if there is anything wrong with my setup/code? Thank you.
It’s been a while and I still have no idea what is causing the problem. For anyone who wishes to see the related code without opening the project, I am posting the content of of MyPawn.cpp here.
AMyPawn::AMyPawn()
{
PrimaryActorTick.bCanEverTick = true;
}
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
for (TActorIterator<AStaticMeshActor> i = TActorIterator<AStaticMeshActor>(GetWorld()); i; ++i)
{ // find cube actor with physics constraint component
UActorComponent *c = i->GetComponentByClass(UPhysicsConstraintComponent::StaticClass());
if (!c) continue;
cube = *i;
pcc = Cast<UPhysicsConstraintComponent>(c);
break;
}
if (pcc) pcc->SetLinearZLimit(LCM_Limited, zLimit = 125); // set initial z-limit
}
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (pcc) DrawDebugLine(GetWorld(), pcc->GetComponentLocation(), pcc->ConstraintActor2->GetActorLocation(), FColor::Red, false); // draw red line
}
void AMyPawn::MoveX(float v)
{
// GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%f"), v));
if (cube && v) cube->SetActorLocation(cube->GetActorLocation() + FVector(0, v, 0)); // move the cube actor along y-axis
}
void AMyPawn::MoveY(float v)
{
// GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("%f"), v));
if (pcc && v) pcc->SetLinearZLimit(LCM_Limited, zLimit = FMath::Clamp(zLimit - v, 0.f, 250.f)); // change z-limit: distance between the cube and the sphere
}
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("X", this, &AMyPawn::MoveX);
PlayerInputComponent->BindAxis("Y", this, &AMyPawn::MoveY);
}