How do I Rigidbody.AddForce(Vector3) in Unreal Engine?

I am coming from the Unity Engine, and trying to get my feet wet with Unreal Engine 4. I want to add a one-time force to an object. To accomplish this in Unity 5, I would use:

void Start(){
    GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, 5));
}

Explanation:

GetComponent() retrieves the Rigidbody Component. The Rigidbody is
either already attached to the GameObject, or this script has
[RequiresComponent(typeof(Rigidbody))] above the class declaration.
The AddForce method is called on the Rigidbody, and applies the force of
a Vector3. The parameters of the Vector3 indicate the force on each
axis which is applied. In the example above, the GameObject would
move forward on its z-axis.

How would I accomplish this same thing in Unreal Engine 4? I would appreciate if you can elaborate on what exactly is happening in the code as well. I currently have an empty AActor script, and I am struggling to find out what to put in my BeginPlay() method.

void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	//What goes here?  Why?
}

UPrimitiveComponent has a UPrimitiveComponent.AddForce(FVector, BoneName) method. However, I cannot figure out how to use it. The real objective of this question is as a basic introduction to programming with Unreal Engine. I chose AddForce, because it is an extremely commonly used method with a visible effect (in Unity). All of the existing tutorials are either with an older version of unreal (which has different scripting) or are using blueprints (which has no scripting). That is why I am trying to figure out what the actual code would be and what that code does.

Specifically, I am looking for an answer which tells me what code is missing from my Unreal Engine script, and what that code does, similiarly as is presented with my Unity Engine example.

So, first off, don’t worry, it’s very simple like in Unity :slight_smile:

The first thing you want to do is have the primitive component be in “Simulate Physics” mode.

In the image above, I have a Static mesh component, with Simulate Physics enabled.

The second part, what you want to add in BeginPlay is this:

void ATestActor::BeginPlay()
{
	Super::BeginPlay();
	StaticMeshComp->AddImpulse(FVector(0.0f, 0.0f, 5000.0f));
}

StaticMeshComp is, of course, the Static Mesh Component for that actor.

I hope this helps you out!

Mick

Thanks! But I am still having some trouble. I dragged my script under StaticMeshComp. Then I tried adding your code to the script. But StaticMeshComp is not resolved. How do I get that variable?
(Mine was named StaticMeshComponent by default, but I tried that as well.)

StaticMeshComp is the name I gave my StaticMeshComponent.
Here is the whole .h

#pragma once

#include "GameFramework/Actor.h"
#include "TestActor.generated.h"

class UStaticMeshComponent;

UCLASS()
class RPGGAME_API ATestActor : public AActor
{
	GENERATED_UCLASS_BODY()
	
public:	
	UPROPERTY(EditInstanceOnly)
	UStaticMeshComponent* StaticMeshComp;

	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;
};

Here is the whole .cpp:

#include "MyGame.h"
#include "Components/StaticMeshComponent.h"
#include "TestActor.h"

// Sets default values
ATestActor::ATestActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	PrimaryActorTick.bCanEverTick = true;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
	StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SMComp"));

	StaticMeshComp->AttachParent = RootComponent;
}

void ATestActor::BeginPlay()
{
	Super::BeginPlay();
	StaticMeshComp->AddImpulse(FVector(0.0f, 0.0f, 10000.0f));
}

void ATestActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}