Go to Top

Opening And Closing A Door Model With Trigger And Scripting In Vanda Engine (Part 2)

Part 2: Opening And Closing The Door

Introduction

In previous tutorial, we set up the door and door frame prefabs. In this tutorial, you learn how to automatically open the door when player approaches and close the door when player walks away. Handling animations in Vanda Engine is very straightforward: You create a trigger, write scripts to manage animations, and attach scripts to enter or exit events of trigger. Script attached to enter event of trigger is executed when the player enters the trigger. When the player stops touching the trigger, exit event is fired which causes exit script to be called. In this tutorial, we use two functions called ExecuteAction() and ReverseExecuteAction() to open and close the door.

Inserting door and door frame Prefabs Into Level

Please fire up Vanda Engine and from the main menu, go to Prefab | Prefab Editor. Locate Door prefab and press Insert Prefab. Open Prefab editor again and this time insert Door Frame prefab into your level.

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image1

Moving The Prefabs Down

Since the bottom of the frame that rests on the floor has a height greater than 0, I prefer to move it down to rest at height 0. Make sure that door frame is selected, choose translate tool by pointing to Translate on the Edit menu, and in lower section of editor, set -0.5 for Y parameter. Repeat the same process for animated door.

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image2

Inserting Trigger Into Level

In order to insert a trigger into level, choose Trigger from Insert menu. A new dialog appears asking you to specify trigger type and name. Give your trigger a name, select Box type, and hit Create button to create and insert a standard box trigger at the center of world coordinates. Since this trigger doesn’t cover an area in front of and behind the door, we have to scale it. Make sure that trigger is selected, then go to Edit | Scale menu, and enter the following values in lower part of editor:

  • X : 7
  • Y : 2
  • Z : 10

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image3

Lua Scripting

Please download and install the Lua package for Windows from Google Code. After installing the package, click the SciTE shortcut in order to run the IDE.

In this tutorial, we use two built-in functions of Vanda Engine named ExecuteAction() and ReverseExecuteAction():

bool     ExecuteAction( char* prefabInstanceName, char* clipName, float delayIn, float delayOut, float weightTarget = 1.0f, bool lockToLastKeyframe = false)

This function executes an animation action.

Parameters

  • prefabInstanceName: The prefab instance name that its animation clip should be executed.
  • clipName: The clip name of the animation cycle that should be executed. If your scene does not contain animation clip, set this parameter to “defaultClip”.
  • delayIn: The time in seconds until the animation action reaches the full weight ( specified by weightTarget ) from the beginning of its execution.
  • delayOut: The time in seconds in which the animation action reaches zero weight at the end of its execution.
  • lockToLastKeyframe: Lets you lock the clip name to its last keyframe at the end of its execution. For example, if you want to open a door, you should tell Vanda Engine to lock the animation to the last keyframe at the end of its execution. Otherwise, the door returns back to it status specified by first keyframe.

Returns

One of the following values:

  • true if successful
  • false if an error happened

In my case, door name located into level is 1_Door_door1.

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image5

So I write the following script:

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image4So I tell Vanda Engine to play the default animation of door model, reach its full weigh (1) after 0.1 seconds, start transiting from full weigh to zero weight just 0.1 second before the end of the animation, and lock the animation to its last keyframe.

Now go to File |  Save menu, give your script file a name, and hit Save button.

Make sure to include .lua at the end of file name. Otherwise, Vanda Engine won’t recognize the script.

In order to assign you script to trigger, return back to Vanda Engine, make sure that trigger is still selected, and press Script button. In Script Manager dialog, press Trigger::Enter, locate your script file, and press open. If If everything was set up correctly, Script Manager reports that script loaded successfully. If you get any errors, return back to IDE, fix the errors, and re-import the script into Vanda Engine.

If you make any changes to your script, you should re-import the script into Vanda Engine.

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image6

Whenever the player leaves the trigger (or in the other word, walks away), we want to play the door animation in reverse order to simulate the close state. ReverseExecuteAction() gets the animation clip specified by its argument and plays it from its current keyframe in reverse order:

bool     ReverseExecuteAction( char* PrefabInstanceName, char* clipName )

Executes an animation action in reverse order. If the specified action is not in ‘action’ status (activated by ExecuteAction function), this function returns false. If the specified action is in action status, but its current keyframe is 0, this function returns false as well.

Parameters

  • prefabInstanceName: The prefab instance name that its animation clip should be executed.
  • clipName: The clip name of the animation cycle that should be executed. If your scene does not contain animation clip, set this parameter to “defaultClip”.

Returns

One of the following values:

  • true if successful
  • false if an error happened

our door model has just one default animation clip, so make sure to pass “defaultclip” to second argument of this function:

Vanda-Engine-Open-Close-Door-Trigger-Scripting-Part2-Image7Save your script, return back to Vanda Engine, and assign this script to Trigger::Exit event of trigger.

So whenever the player touches the trigger, the animation of door model is played from beginning to end and locked to its last keyframe. When the player leaves the trigger, door animation is played in reverse order from end keyframe to first keyframe. If the player leaves the trigger before ending the animation, ReverseExecuteAction() playes the animation from current active keyframe of animation in reverse order.

arrow-leftPrevious Tutorial

Next Tutorial arrow-right

Let’s stay in touch!

Never miss a single tutorial, sign up for our monthly newsletter. You can unsubscribe at any time if you wish to stop receiving email newsletters from vandaengine.org

 

Leave a Reply

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

Simple Share Buttons