How to control Human Movement in 3DEXCITE Using JavaScript

13 February 2025 6 mins to read
Share

Keyboard Controlled Human Movement in 3DEXCITE

In the 3DEXCITE Creative Experience application, you can make a human character move along a predefined path when a key on the keyboard is pressed. To accomplish this, we start by setting up motion behavior, then proceed to scripting an event, and finally configure a scenario in Storytelling. In this guide, we will walk you through the process of enabling keyboard-controlled human movement in 3DEXCITE, allowing precise control over your actor’s movements.

Let’s go through the process step by step!

Step 1:Import the Human Model

First of all, we need to bring a human character into the scene.

  1. Open your scene in Creative Experience.
  2. then, Click Import and select a human model from the available assets.
  3. Once the model is imported, you should then position the human where you want them to start.

Step 2: Add Motion Behavior

Now that the human model is in place, we can shift our focus to adding motion behavior.

  1. First, Select the human object in the scene.
  2. then, Go to the Properties tab.
  3. After, Click Add Library and choose Human Motion Behavior.
  4. Assign the behavior to the human model.

As a result, the human will be able to move along a path in response to our scripted event.

Figure 1 : Human Motion Behavior – Creative Experience

Step 3: Create a Path for the Human

Before we move on to scripting the movement, let’s first define the path the human will follow.

  1. First, Go to the Essentials tab.
  2. then, Click Path and start drawing your path in the scene.
  3. If necessary, adjust the path shape to ensure smooth movement.
  4. Lastly, Save the path and make sure it is properly connected to the scene.

This path will serve as the movement guide when the script is triggered.

Figure 2 : Create a Path for the Human

JavaScript Techniques for Keyboard Input in 3DEXCITE

Step 4: Create a Scripted Actor

At this point, we need to set up a scripted actor to handle keyboard input and trigger the human’s movement.

Figure 3: Creating a JavaScript Scripted Actor
  1. Start by Selecting the Create Scripted Actor in the Logic Actors .
  2. Copy and paste the following JavaScript code into the script editor:

var TriggerEventClass = function () {
EP.Event.apply(this, arguments);
};

TriggerEventClass.prototype = Object.create(EP.Event.prototype);
TriggerEventClass.prototype.constructor = TriggerEventClass;
TriggerEventClass.prototype.type = 'TriggerEventType';

if (!STU.TriggerEvent) {
STU.TriggerEvent = TriggerEventClass;
}

if (!EP.EventServices.getEventByType(TriggerEventClass.prototype.type)) {
EP.EventServices.registerEvent(TriggerEventClass);
}

beScript.onStart = function () {
// Code to execute when the script starts.
};

beScript.onStop = function () {
// Code to execute when the experience stops.
};

beScript.execute = function (context) {
// Executed on each frame.
// 'context.deltaTime' provides the time elapsed since the last frame.
};

beScript.onAllKeyboardRelease = function (iEvent) {
// Uncomment the following line to debug key releases.
// console.log(iEvent.key);

if (iEvent.key === this.KeyCode) {
let newEvent = new STU.TriggerEvent();
this.actor.dispatchEvent(newEvent);
}
};

 

Step 5: Understanding the JavaScript Code

Let’s break down what this script does and how it works in Creative Experience.

1️⃣ Creating a Custom Event

var TriggerEventClass = function () {
EP.Event.apply(this, arguments);
};

This defines a custom event class called TriggerEventClass. It extends the EP.Event base class, making it recognizable as an event in Creative Experience.

2️⃣ Setting Up the Event Prototype

TriggerEventClass.prototype = Object.create(EP.Event.prototype);
TriggerEventClass.prototype.constructor = TriggerEventClass;
TriggerEventClass.prototype.type = 'TriggerEventType';

Here, we create an object prototype based on EP.Event and set a custom event type, TriggerEventType.

3️⃣ Registering the Custom Event

if (!STU.TriggerEvent) {
STU.TriggerEvent = TriggerEventClass;
}

if (!EP.EventServices.getEventByType(TriggerEventClass.prototype.type)) {
EP.EventServices.registerEvent(TriggerEventClass);
}

In order to ensure proper functionality, this first checks if STU.TriggerEvent exists. If it doesn’t, it then assigns our TriggerEventClass to it. It also ensures that TriggerEventType is registered in Event Services so that it can be triggered later.

4️⃣ Event Lifecycle Functions

beScript.onStart = function () {
// Code to execute when the script starts.
};

beScript.onStop = function () {
// Code to execute when the experience stops.
};

beScript.execute = function (context) {
// Executed on each frame.
// 'context.deltaTime' provides the time elapsed since the last frame.
};

These functions define the lifecycle of the script:

  • onStart: Runs when the script starts.
  • onStop: Runs when the experience stops.
  • execute: Runs every frame (useful for continuous actions).

5️⃣ Handling Keyboard Input

beScript.onAllKeyboardRelease = function (iEvent) {
// Uncomment the following line to debug key releases.
// console.log(iEvent.key);

if (iEvent.key === this.KeyCode) {
let newEvent = new STU.TriggerEvent();
this.actor.dispatchEvent(newEvent);
}
};

This function is triggered whenever a key is released. It checks if the released key matches our KeyCode. If it does, it creates a new instance of the custom event (STU.TriggerEvent) and dispatches it to the actor.

Step 6: Configure the Sensor

Next, to detect the key press event, we need to configure a sensor.

  1. First, Go to the Sensors tab and create a new sensor.
  2. then, Set the Type to EVENT.
  3. After, Set the ID to TriggerEventType.
  4. Set the Alias to Executed.
  5. Click the Play icon to activate the sensor.
Figure 4 : Create Sensor

Define a KeyCode Property

In other words, to specify which key triggers the movement:

  1. In the Properties tab, click Add New Property.
  2. Set Name to KeyCode.
  3. Set Type to Integer.
  4. Set Value to 73 (which corresponds to the “I” key in the JavaScript API Guide).
Figure 5 : Create KeyCode Properties

Step 7: Create a Storytelling Scenario

After setting up the sensor, we now define how the human reacts to the event.

  1. Click Create Scenario under the Storytelling tab.
  2. Add a new “Each time Script Actor is Executed” block.
  3. Set the action to Human Activate.
  4. Then, Add another block for Human Follow Path.
  5. Link them together so that when the event occurs, the human activates and follows the path.
Figure 6 : Create Storytelling Scenario

Step 8: Run the Experience

You’re all set! Now, let’s test it:

  • Click Play to run the simulation.
  • Press I on your keyboard.
  • After, The human should now start following the path.

Conclusion

In this tutorial, we have walked through the process of:

  • Importing a human model into the scene.
  • Adding motion behavior to enable movement.
  • Creating a path for the human to follow.
  • Writing a JavaScript script to detect keyboard input.
  • Configuring a sensor to trigger an event.
  • Lastly, Using Storytelling to make the human move when the “I” key is pressed.

Furthermore, this method can be expanded to create more complex interactions. Such as triggering animations, controlling multiple characters, or integrating additional events.

Finally, in the next blog, we’ll explore automating postures and gestures using JavaScript.

Human Postures in 3DEXCITE

1 Comment on “How to control Human Movement in 3DEXCITE Using JavaScript”

Leave a comment

Your email address will not be published. Required fields are marked *