This tutorial is for intermediate developers who already know how to create objects and edit scripts in Unity 3D.
Resources for this tutorial:
- Unity 3D: https://unity.com/games
- LUMOplay's Unity SDK: https://signup.lumoplay.com/sdk-request
- Mole blend file with textures and timing sheet: https://www.dropbox.com/scl/fo/gog5n237t4kre8dkhzemb/AJao-trL4RfG6HhpjZRIlBE?rlkey=7eami9z93afqqzcwv5i7sot9p&dl=0
Code snippet used in this tutorial:
using LUMOplay;
using UnityEngine;
public class TriggerAnimation : MonoBehaviour
{
// Animator with the Trigger to set
[SerializeField] Animator animator;
// Trigger name on the animator
[SerializeField] string triggerName;
// How long before the trigger can be set again (in seconds)
[SerializeField] float timeout;
// this tracks when the Trigger was last used, defaults to negative 10 minutes so it can be triggered as soon as the scene loads
float lastTriggered = -600f;
// Trigger the animation if the timeout isn't pending
public void TriggerNow(LumoMotionEvent evt)
{
if (Time.time - lastTriggered > timeout)
{
lastTriggered = Time.time;
animator.SetTrigger(triggerName);
}
}
}1. Create a project
Create a basic 3D project in Unity.
2. Import LUMOplay's Unity 3D SDK
Importing the samples included with the SDK is optional.
3. Make a plane
Create a plane and scale it up to create a nice big work area.
4. Make a placeholder
Create a temporary cube and move it to the area where your animation will be placed.
5. Adjust your game camera
Move the camera so the cube is in the center of the scene. Positioning the camera slightly up and pointing down at an angle will give the best result.
6. Import Mole.blend
Create a new folder called "3D" in the Unity Assets folder and make a folder inside it called "Mole". Drag the Mole.blend file and texture folder into the 'Mole' folder.
7. Extract the mole materials
Set up a folder for the mole materials. Save your import changes. Make any material adjustments you want.
8. Set up the animations
Open the animation timing sheet. For this example, we only need 3 animations:
- Appear state
- Idle state
- Vanish state
Select the mole, rename the first scene to "Appear", and select frames 237 to 254. (Ignore the bag of money, that'll remain hidden beneath the floor Plane). Double check and make sure the Scene is not set to loop.
Create a new Scene called "Vanish", this one is also non-looping, and includes frames 310 to 342.
Create a third Scene called "Idle Loop". Set looping to 'Enabled'. The Idle loop includes frames 255 to 308. Hit 'Apply'.
Now we have our 3 animations. Vanish, Appear and our Idle loop.
9. Add the mole to the main game scene
Add the mole to the plane by dragging the model up into the SampleScene window and setting the model to be at the same location as the temporary cube. Resize and adjust the model orientation so it looks good in the game camera and delete the temporary cube.
10. Animate the mole
Drag the 'Vanish' clip onto the model. Unity will create an Animator. Scroll through the frames to watch him Vanish.
In the Animator viewport, drag the 'Idle' and 'Appear' animations onto the model as well. Right-click to set the 'Appear' animation to be the Default state; set that to transition to the 'Idle' animation, set 'Idle' to transition to 'Vanish', set 'Vanish' to transition back to his 'Appear' animation.
Set the Exit Time on the 'Appear' animation to 0.8
Set the 'Transition' Duration to 0.1.
11. Set up a test interaction trigger
Create a new Trigger and name it "Vanish". The Vanish animation has no Exit Time, so uncheck that. Set the Transition Duration to 0.1. Add a Condition. This will be linked to when the 'Vanish' Trigger is activated.
12. Add a transition delay
For the final transition from Vanish back to Appear, increase the Exit Time to 1.5 to give the animation a slight delay after the mole appears. It will remain unseen for half a cycle before it reappears.
13. Save the project and test the game scene
The mole should pop out and stay there until the animation is triggered by clicking it with your mouse. I'm just manually activating the Vanish trigger, and the whole scene plays out how I want it to. We don't want to manually trigger the animation though, we want it to be triggered by motion input, so the next step is to
14. Link LUMOplay's motion detection system
Add a new Component and give it a Sphere Collider. Adjust the height and size of the Sphere Collider to match the area that the Mole's character model occupies when it is above ground.
15. Test the Collider
Add another New Component and choose 'On Motion 3D' from the LUMOplay SDK. Within 'On Motion 3D', create a New Event, and drag the Mole's Animator inside that event. Set the Event's function to 'SetTrigger (string)', and then set it to trigger the 'Vanish' Trigger.
NOTE: Technically everything should be linked up now, but there are 2 problems. The first problem is that if we go to test the scene, we will get an error. This is because Unity 6 has introduced a new input system that we will need to change. In our Project Settings, go to 'Other Settings' and scroll down until we find the Active Input Handling. Change this back to the Old Input Manager, or select 'Both'. Unfortunately, this will cause a reload of the software. I'll apply and save, and then the Unity will have to restart. This will be addressed in the next version of the LUMOplay SDK.
16. Create a script
Click on the mole. He should disappear twice, because you are now using LUMOplay's vision system. The mouse cursor now creates motion events across multiple frames of the animation, the same way motion captured by a camera will in your final installation.
This means that the 'Vanish' trigger is now getting immediately consumed and transitioning through to the beginning before the actual animation has had a chance to play out in full, causing it to repeat itself. To avoid this, add a simple time-out using a custom script.
Remove the test event and create a new Script called "TriggerAnimation". Click on New Script, click on Create and Add, and set up a Scripts folder in the Unity Assets folder to save it in.
17. Open your script editor
Double clicking the new Script will open it in your script editor (we use Microsoft Visual Studio). Copy-paste this code:
using LUMOplay;
using UnityEngine;
public class TriggerAnimation : MonoBehaviour
{
// Animator with the Trigger to set
[SerializeField] Animator animator;
// Trigger name on the animator
[SerializeField] string triggerName;
// How long before the trigger can be set again (in seconds)
[SerializeField] float timeout;
// this tracks when the Trigger was last used, defaults to negative 10 minutes so it can be triggered as soon as the scene loads
float lastTriggered = -600f;
// Trigger the animation if the timeout isn't pending
public void TriggerNow(LumoMotionEvent evt)
{
if (Time.time - lastTriggered > timeout)
{
lastTriggered = Time.time;
animator.SetTrigger(triggerName);
}
}
}18. Create a Motion Event
Create a new Motion Event for the Mole character. Drag in 'TriggerAnimation', and set the function to 'TriggerNow', which has been pushed right to the top of the menu thanks to our custom script.
Connect the Animator to the TriggerAnimation script by dragging the Mole's Animator into the Animator field. Link it to the 'Vanish' Trigger. Add a time-out of 2.5.
The mole should disappear and comes back without the animation being triggered twice.
19. Make more moles
Duplicate the mole and move it until you have a whole field of moles that can be triggered with motion.
20. Test with a camera
Open LUMOplay software and calibrate your camera. Testing the mole game scene in Unity with LUMOplay running will connect straight to the live motion feed within LUMOplay. You will now be able to trigger the animation by moving in front of the camera.
From here you can go on to make an even more complicated Whack-a-Mole style game. If you have any questions, or have any requests for future LUMOplay SDK tutorials, please leave a comment.