Main Content

Animate Custom Actors in the Unreal Editor

Follow these steps to animate a custom actor in the Unreal® Editor. Before you start, make sure you that you have Visual Studio® 2019 and the Vehicle Dynamics Blockset™ Interface for Unreal Engine® 4 Projects support package installed on your machine. For more information, see Install Support Package and Configure Environment.

Additionally, make sure that:

  • You are comfortable coding with C++ in Unreal Engine.

  • Your Unreal Editor C++ project contains a skeletal actor mesh. This example uses a bicycle mesh.

    Unreal Editor bicycle mesh

This examples provides the workflow for animating a bicycle actor. The general workflow is adapted from the Unreal Engine Vehicle User Guide.

Set up Simulink Model

Step 1: Set up Simulink Model

Open a new Simulink® model and add these blocks:

  • Two Ramp blocks

  • Constant block

  • Simulation 3D Actor Transform Set block

  • Simulation 3D Scene Configuration block

Connect and name the blocks as shown.

Simulink model with connected blocks

Step 2: Configure Blocks

Configure blocks with these parameter settings.

BlockParameter Settings

Simulation 3D Scene Configuration

  • Scene sourceUnreal Editor

  • Project — Name and location of the installed support package project file, for example, C:\Local\AutoVrtlEnv\AutoVrtlEnv.uproject.

  • Scene viewScene Origin

Simulation 3D Actor Transform Set

  • Actor Setup tab:

    • Tag for actor in 3D scene, ActorTagBike1

      Note

      This tag should match the Unreal Editor tag name in Step 6: Instantiate the Bicycle Actor.

    • Number of parts per actor to set, NumberOfParts3

  • Initial Values tab:

    • Initial array values to translate actor per part, Translation[0 0 0;0 0 0;0 0 0]

    • Initial array values to rotate actor per part, Rotation[0 0 0;0 0 0;0 0 0]

    • Initial array values to scale actor per part, Scale[1 1 1;1 1 1;1 1 1]

Translation Ramp

  • Slope[0.35 0 0;0 0 0;0 0 0]

Rotation Ramp

  • Slope[0 0 0;0 -pi/5 0;0 -pi/5 0]

Scale Constant

  • Constant value[1 1 1;1 1 1;1 1 1]

Set up Unreal Editor to Animate Bicycle

Step 3: Set up Animation Instance

  1. In your Simulink model, use the Simulation 3D Scene Configuration block Open Unreal Editor parameter to open the Unreal Editor.

  2. Select File > New C++ Class. In the Choose Parent Class dialog box, select Show All Classes. Search for AnimInst. Add the AnimInstance parent class.

    Unreal Editor choose parent class

  3. Name the new C++ class SimulinkBikeAnimInst. Select Public. Click Create Class.

    Unreal Editor name parent class

  4. In Visual Studio 2019, open the C:\Local\AutoVrtlEnv\AutoVrtlEnv.sln file. Navigate to the SimulinkBikeAnimInst.cpp and SimulinkBikeAnimInst.h source files.

    Visual studio solution explorer

    Edit the files as shown.

    Tip

    For this example, the code includes FWheelRotation and RWheelRotation properties to animate the bicycle wheel rotation. You can add additional properties to animate other parts of the bicycle.

     Code: SimulinkBikeAnimInst.h

     Code: SimulinkBikeAnimInst.cpp

  5. In the Unreal Editor click Compile.

    Unreal editor compile

Step 4: Create Animation Blueprint

  1. In your Simulink model, use the Simulation 3D Scene Configuration block Open Unreal Editor parameter to open the Unreal Editor.

  2. In the Unreal Editor, on the Content Browser tab, under View Options, select Show Engine Content and Show Plugin Content.

    Unreal Editor options

  3. Add the animation mesh. On the Content Browser tab, navigate to MathWorksSimulation Content > Vehicles > Bicyclist > Meshes.

    Unreal Editor content browser

  4. Select Add New > Animation > Animation Blueprint.

    Unreal Editor add animation blueprint

  5. In the Create Animation Blueprint dialog box, select:

    • Parent Class: SimulinkBikeAnimInst

    • Target Skeleton: SK_Bicycle_Skeleton

    Unreal Editor create animation blueprint

    Click OK.

  6. Name the blueprint BikeAnimation. Right-click and select Save.

    Unreal Editor save animation blueprint

  7. Open the BikeAnimation blueprint. Make the connections as shown.

    Unreal Editor blueprint connections

    Make sure that you set:

    • Bone to Modify to the correct bone

    • Rotation Mode to Replace Existing

    • Rotation Space to Bone Space

    Unreal Editor blueprint specifications

  8. Compile and save the blueprint.

Step 5: Create Bicycle Actor C++ Class

  1. In the Unreal Editor, on the Content Browser tab, under View Options, select Show Engine Content and Show Plugin Content.

    Unreal Editor options

  2. From the MathWorksSimulation C++ Classes folder, select Sim3dActor.

    Unreal Editor content browser

    Right-click and select Create C++ class derived from Sim3dActor.

    Unreal Editor class actions

    Tip

    If you do not see the MathWorksSimulation C++ Classes folder, use these steps to check that you have the MathWorksSimulation plugin installed and enabled:

    1. In the Unreal Editor toolbar, select Edit > Plugins.

    2. In the Plugins window, verify that the MathWorks Interface plugin is listed in the installed window. If the plugin is not already enabled, select the Enabled check box.

      If you do not see the MathWorks Interface plugin in this window, repeat step 3 in Configure Environment and reopen the editor from Simulink.

    3. Close the editor and reopen it from Simulink.

  3. Name the new Sim3dActor BicycleActor. Select Public. Click Create Class.

    Unreal Editor class name

  4. In Visual Studio, navigate to BicycleActor.h and BicycleActor.cpp.

    Visual studio solution explorer

    Edit the files as shown.

    Tip

    For this example, the code includes logic to animate the bike body (BIKE_BODY), front wheel (FRONT_WHEEL), and rear wheel (REAR_WHEEL). You can add additional logic to animate other parts of the bicycle.

     Code: BicycleActor.h

     Code: BicycleActor.cpp

  5. In the Unreal Editor click Compile.

    Unreal editor compile

Step 6: Instantiate the Bicycle Actor

  1. In your Simulink model, use the Simulation 3D Scene Configuration block Open Unreal Editor parameter to open the Unreal Editor.

  2. Place the Bicycle Actor in the scene.

    Unreal editor bicycle actor placement

  3. Set the tag to the same value as the Simulation 3D Actor Transform Set block Tag for actor in 3D scene, ActorTag. For this example, set the value to Bike1.

    Unreal editor set tag value

Set up Camera View (Optional)

Optionally, set up a camera view to override the default view. You can use either Simulink or a level blueprint to set up the camera view. For the recommended option, use Simulink.

Step 7: Use Simulink (Recommended)

To setup a camera view that follows along with the bicycle:

  1. Add these blocks to the model.

    • One Ramp block

    • One Add block

    • Three Constant blocks

    • Simulation 3D Actor Transform Set block

    Connect and name the blocks as shown.

    Simulink model block connections

  2. Set these block parameters.

    BlockParameter Settings

    Simulation 3D Actor Transform Set: Camera Control

    • Tag for actor in 3D scene, ActorTagMainCamera1

    CamTranslation

    • Constant value[0 -5.1 0.56]

    • Interpret vector parameters as 1-Doff

    CamRotation

    • Constant value[0 0 deg2rad(85)]

    • Interpret vector parameters as 1-Doff

    CamScale

    • Constant value[1 1 1]

    • Interpret vector parameters as 1-Doff

Step 7: Use Level Blueprint

To override the default camera view:

  1. Add a camera actor. Assign it as a child of the BicycleActor.

  2. Use the Transform settings to specify the location and viewing angle.

    Unreal Editor set actor transformation settings

  3. Open the level blueprint.

    Unreal Editor open level blueprint selection

  4. In the level blueprint, make these connections. If you right-click on the Event Graph to find nodes, clear Context Sensitive. If you have a CameraActor, you can drag it to the Event Graph from the World Outliner view in the editor.

    Unreal Editor blueprint connections

  5. Save the blueprint and project. Close the Unreal Editor.

Run Simulation

After you configure the Simulink model and Unreal Editor environment, run a simulation.

  1. In your Simulink model, make sure that you have set the Simulation 3D Scene Configuration parameters to these values:

    • Scene sourceUnreal Editor

    • Project — Name and location of the installed support package project file, for example, C:\Local\AutoVrtlEnv\AutoVrtlEnv.uproject.

    • Scene viewScene Origin

  2. Use the Simulation 3D Scene Configuration block Open Unreal Editor parameter to open the Unreal Editor.

  3. Run the simulation.

    1. In the Simulink model, click Run.

      Because the source of the scenes is the project opened in the Unreal Editor, the simulation does not start.

    2. Verify that the Diagnostic Viewer window in Simulink displays this message:

      In the Simulation 3D Scene Configuration block, you set the scene source to 'Unreal Editor'. In Unreal Editor, select 'Play' to view the scene.

      This message confirms that Simulink has instantiated the vehicles and other assets in the Unreal Engine 3D environment.

    3. In the Unreal Editor, click Play. The simulation runs in the scene currently open in the Unreal Editor.

See Also

|

Related Topics

External Websites