7
\$\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\$
0

3 Answers 3

2
\$\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

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

If you want to add a one-time force to the object you should use the AddImpulse function instead of the AddForce, because AddForce applies a force continually.

To use the AddImpulse function you need the static mesh of the AActor first because is the one that contains the physics. Also check if the mesh has the Collisions/Physics active in the viewport of the Blueprint.

If you come from Unity, by looking at the Vector you use to apply the Force, you may want to move it in the Forward axis, but take into consideration that in Unreal Engine the Forward axis is the Y not the Z so you may want to use the FVector(0.0f, 5.0f, 0.0f). In case that wasn't your intention, I've used the exact same Vector you used in Unity in this sample code.

Here is an example of what you could do:

void AMyActor::BeginPlay()
{
    Super::BeginPlay();

    //Get the static mesh of the chosen Actor
    UStaticMeshComponent* StaticMesh = Cast<UStaticMeshComponent>(GetRootComponent());
 
    //If the static mesh is valid apply the given force
    if (StaticMesh)
    {
        /*Create the Vector with the force to use*/
        FVector ForceToAdd = FVector(0.0f, 0.0f, 5.0f);
    
        /*When you want to apply a force you always need to multiply it's value by the
        mass of the object that the object is applied to.*/
        StaticMesh->AddForce(ForceToAdd * StaticMesh->GetMass());
    }
}

You can find more info here: https://www.orfeasel.com/adding-force-to-an-actor/

\$\endgroup\$
-2
\$\begingroup\$

I think this is what you want:

It's adds a movement vector to the pawn.

\$\endgroup\$
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .