How to Add Force using C++

I’m trying to add this through a scene component. Anyone be able to help me do this in C++?

Thanks!

There are some ways to do this depending on how you are receiving the inputs. The easiest way is to set the axis binding in the pawn controller. If for some reason you have to do it in a different actor it can be done this way:

void AAddForceOnKeyActor::BeginPlay()
{
	Super::BeginPlay();
	APlayerController* PlayerController = Cast<APlayerController>(GetWorld()->GetFirstPlayerController());
	if (PlayerController) {
		EnableInput(PlayerController);
		InputComponent->BindAxis("MoveForward", this, &AAddForceOnKeyActor::AddForce);
	}
}

To add the force, you need to add the reference to the actor first.

//On the actor.h file
UPROPERTY(EditAnywhere) 
AActor* TestShipActor;

The addforce function will look something like this, casting the primitivecomponent of the actor:

void AAddForceOnKey::AddForce(float Value)
{
	if (Value > 0 && TestShipActor != nullptr) {
		UPrimitiveComponent* TestShip = TestShipActor->FindComponentByClass<UPrimitiveComponent>();
		FVector forceVector = GetActorForwardVector() * -99999.0f * Value;

		// If we hit an actor, with a component that is simulating physics, apply an impulse  
		if ((TestShip != nullptr) && TestShip->IsSimulatingPhysics())
		{
			TestShip->AddForce(forceVector);
		}
		GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, GetActorForwardVector().ToString());
	}
}