How to control Human Movement in 3DEXCITE Using JavaScript

13 February 2025 6 mins to read
Share

Keyboard Controlled Human Movement in 3DEXCITE

In 3DEXCITE Creative Experience application, we can make a human character move along a predefined path when a key on the keyboard is pressed. This involves setting up motion behavior, scripting an event, and configuring a scenario in Storytelling. This guide will walk you through keyboard-controlled human movement in 3DEXCITE, allowing precise control over your actor’s movement.

Let’s go through the process step by step!

Step 1: Import the Human Model

The first step is to bring a human character into the scene.

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

Step 2: Add Motion Behavior

Now, we need to add movement capabilities to the human model.

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

This will allow the human 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

Next, we define the path that the human will follow.

  1. Go to the Essentials tab.
  2. Click Path and start drawing your path in the scene.
  3. Adjust the path shape as needed.
  4. 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

Now, we set up a scripted actor to handle keyboard input and trigger the human’s movement.

Figure 3: Creating a JavaScript Scripted Actor
  1. Select 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

This script is responsible for detecting a key press event and triggering an action in Creative Experience. Let’s break it down:

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, allowing it to be recognized 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 the EP.Event prototype.
  • We specify a custom event type called "TriggerEventType".

3️⃣ Registering the Custom Event

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

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

  • This checks if STU.TriggerEvent exists. If not, it assigns our TriggerEventClass to it.
  • It also ensures that "TriggerEventType" is registered in the 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);
}
};

  • onAllKeyboardRelease 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

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

  1. Go to the Sensors tab and create a new sensor.
  2. Set the Type to EVENT.
  3. 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

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

Now, we 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. 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.
✅ The human should now start following the path.

Conclusion

In this tutorial, we used 3DEXCITE Creative Experience app to:

  • Import a human model into the scene.
  • Add motion behavior to enable movement.
  • Create a path for the human to follow.
  • Write a JavaScript script to detect keyboard input.
  • Configure a sensor to trigger an event.
  • Use Storytelling to make the human move when “I” is pressed.

This approach can be expanded for more complex interactions, such as triggering animations, controlling multiple characters, or integrating additional events.

In the next blog, we’ll take things further by automating postures and gestures using JavaScript. Stay tuned—it’s going to be exciting!

Human Postures in 3DEXCITE

Leave a comment

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