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.
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.
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
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():
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.
So I write the following script:
So 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.
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.
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:
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:
Save 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.
(c) copyright Zehne Ziba Co., Ltd. For more information, please refer to copyright notice.