Hello,
I just started learning Unreal Engine and I have a strange behaviour.
I’m working with a physical constraint component linking two cubes. The constraint is configured with angular velocity, but I’m encountering inconsistent behavior:
- Initialization:
- If I set a non-zero target velocity (e.g.,
(0.0, 0.0, 0.1)) in the editor, the rotation starts as expected, but after stopping it via code, the rotation won’t restart. - If initialized with
(0.0, 0.0, 0.0), it doesn’t spin at all when I try to apply a velocity target programmatically.
- Code Behavior:
- Using
SetAngularVelocityTargettoggles the velocity between(0.0, 0.0, 0.1)(to spin) and(0.0, 0.0, 0.0)(to stop). - The constraint spins correctly the first time but fails to restart after stopping.
Here is my setting in the editor
And this is my Code Snipped:
RotationComponentForActor.h
#pragma once
include “CoreMinimal.h”
include “Components/ActorComponent.h”
include “PhysicsEngine/PhysicsConstraintComponent.h”
include “RotationComponentForActor.generated.h”
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API URotationComponentForActor : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component’s properties
URotationComponentForActor();
int counter = 0;
int counter2 = 0;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
RotationComponentForActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
include “UObject/NameTypes.h”
include “Components/SkeletalMeshComponent.h”
include “RotationComponentForActor.h”
// Sets default values for this component’s properties
URotationComponentForActor::URotationComponentForActor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don’t need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void URotationComponentForActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void URotationComponentForActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
counter++;
if((1/DeltaTime*2) < counter )
{
TSet<UActorComponent*> Components = GetOwner()->GetComponents();
for (UActorComponent* Component : Components)
{
//UE_LOG(LogTemp, Warning, TEXT("Component: %s!"), *Component->GetName());
if (UPhysicsConstraintComponent *buffer = Cast<UPhysicsConstraintComponent>(Component)) //if (USkeletalMeshComponent * Constraint = Cast<USkeletalMeshComponent>(Component))
{
if(counter2%2)
{
UE_LOG(LogTemp, Display, TEXT("mod2"));
buffer->SetAngularDriveMode(EAngularDriveMode::TwistAndSwing);
buffer->SetAngularOrientationDrive(true,false);
buffer->SetAngularDriveParams(10.0f, 180.0f, 0.0f); // Strength, damping, force limit
buffer->SetAngularVelocityTarget(FVector(0.0, 0.0, 0.8));
counter2++;
}
else
{
UE_LOG(LogTemp, Display, TEXT("mod1"));
buffer->SetAngularVelocityTarget(FVector(0.0, 0.0, 0.0));
counter2++;
}
counter=0;
}
}
}
// ...
}

