I’ve found this comment about how to implement logic in the physics sub step. However I have trouble implementing that solution.
I figured I have to create a custom component and override the TickComponent function.
#pragma once
#include "Components/StaticMeshComponent.h"
#include "CustomMeshComponent.generated.h"
/**
*
*/
UCLASS()
class Custom_API UCustomMeshComponent : public UStaticMeshComponent
{
GENERATED_BODY()
UCustomMeshComponent();
~UCustomMeshComponent();
public:
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
void CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance);
};
#include "Custom.h"
#include "CustomMeshComponent.h"
UCustomMeshComponent::UCustomMeshComponent() : UStaticMeshComponent(FObjectInitializer::Get()) {
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bStartWithTickEnabled = true;
PrimaryComponentTick.TickGroup = TG_PrePhysics;
Activate();
UE_LOG(LogTemp, Warning, TEXT("Constructed"));
}
UCustomMeshComponent::~UCustomMeshComponent() {
}
void UCustomMeshComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
FCalculateCustomPhysics onphysics;
onphysics.BindUObject(this, &UCustomMeshComponent::CustomPhysics);
GetBodyInstance()->AddCustomPhysics(onphysics);
UE_LOG(LogTemp, Warning, TEXT("Tick got registered."));
}
void UCustomMeshComponent::CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance) {
UE_LOG(LogTemp, Warning, TEXT("Ticktime %f"), DeltaTime);
BodyInstance->AddForce(FVector(1000, 0, -8000), false);
// also put a breakpoint here
}
I added the component to a pawn and assigned a mesh to it. SimulatePhysics has been enabled as well. But neither does something get logged (except “Constructed”), nor does the actor move. It looks like TickComponent never gets called. SubStepping is enabled in the project properties as well.
Sadly I can’t find much information about this functionality in the documentation or Google. Does anyone know more?