6
\$\begingroup\$

I am coming from the Unity Engine, and trying to get my feet wet with Unreal Engine 4.8.x. 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<Rigidbody>() retrieves the Rigidbody Component. The Rigidbody is already attached to the GameObject. 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?
}

As CobaltHex pointed out, 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.

\$\endgroup\$
1
\$\begingroup\$

See https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/index.html

It has all kinds of functions (Similar to unity). One being AddForce.

Also, some physics is handle by Nvidia's Physx but I don't know how much of that you would interact with

| improve this answer | |
\$\endgroup\$
-2
\$\begingroup\$

I think this is what you want:

It's adds a movement vector to the pawn.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.