Creating a Motion Path and a Rail Track in Blender
Ok, on Part 01 we setup our little VR Project in Unity. Now, we will use Blender to create a rail track around the Island and also to create an object that will follow along that track so we can connect our VR Camera to it later.
- Open Blender, delete all the default scene objects and import the LowPolyIsland asset it into your scene (you’ll find this file in the Assets folder). Save your file as MotionPath.blend;
- Now let’s setup the correct frame rate and duration for our animation. Go to the Properties panel and click the Render icon (camera icon). In the Dimensions row choose 60 fps for the Frame Rate and 1200 for End Frame;
- Back in your scene lets add a Path, hit the shortcut Shift+A and select Curve>Path from the dropdown menu.
Rotate the Path -90 degrees on Z axis so you can start leveled with the ground, also, reposition it at Z:0.25. Enter Edit Mode (Tab key), select all points (A key) and move the path along the Y axis so his first point passes over the object’s origin point (G to activate Grab/Move and Y to move it along the Y axis).
Select the starting point (you can determine the starting point by the arrows direction) and change it’s Y location to 0. Now you can start extruding the other extremity to create your own track around the Island (E to extrude the path). Your last point must also end at 0 coordinates;
- After you have your path going around the island lets change some of its properties. In Edit Mode you will see that the direction arrows twist along its curves, that is something that must be avoided because we don’t want to induce nausea in anybody that will try our roller coaster. In the Properties panel, click the Object Data tab (the button with a icon of a curve), under the Twisting type select Z-Up. All the arrows will now be flat on the Z axis. Also, under the Path Animation row increase the number of frames to 1200;
- Now we need an object that will travel along our Path. Add a 1×1 Plane to your scene (Shift+A>Mesh>Plane) and reposition it at Z:2. Enter Edit Mode (Tab key) select the two vertices with negative Y and center merge them (Alt+M>At Center). Select all 3 vertices and recenter the object at 0 coordinates.
Exit Edit Mode, click Constraints in the Properties panel and add a Follow Path constraint. Select your NurbsPath as the Target and tick Follow Curve.
Now if you press Play in the Timeline panel the arrow will follow the path. Go ahead and delete the Island, the Water and the Ruins. Don’t forget to save your file;
- Now let’s use the current file to create the rail track. Press File>Save As and name it RailTrack. In this new file you wont be needing the Plane object so delete it. Hit Shift+A to add a new Cube, rename it to Rails and model it into a rail track section.
Once you have it modeled, in the Properties panel, go to the Modifiers tab and add an Array modifier, choose Fit Curve for the Fit Type and choose your path for the Curve. Now add a Curve modifier and select your path for the Object.
If everything went well you should have a complete rail track modeled around your NurbsPath. Don’t forget to save. Finally export your rail track as FBX into your Unity project’s Assets folder.
Importing and setting up everything in Unity
Now, that we have everything we need from Blender, it’s time to get back into Unity.
- Open your Unity project. From the Project tab go trough Assets>RailTrack, drag and drop the Rails asset into your scene and reposition it at 0 coordinates. Next we need to import the MotionPath.blend file into your scene (top menu: Assets>Import New Asset…). Again, from the Project tab go trough Assets>MotionPath, drag and drop the Plane prefab asset into your scene and reposition it at these coordinates: X0,Y0.25,Z0. With the Plane prefab still selected drag the object Default Take from the Project panel into the Inspector panel. the Plane’s Animator component will automatically update his Controller;
- Finally we will be able to connect our Camera to the Plane object. First, in the Hierarchy panel, make CardboardMain a child of Plane (parent), drag the CardboardMain into the Plane prefab.
With CardboardMain selected go the Inspector panel and change its coordinates to X0,Y2,Z0, also, press Add Component and select Physics>Rigidbody. Change the Mass and Angular Drag to 0 and untick Use Gravity (Mass will automatically change to 1e-07).
Add the same physics component to Plane (child) and apply the same values to it. With Plane (child) still selected press Add Component again but this time choose Physics>Configurable Joint. Change the Connected Anchor coordinates to X0,Y-2,Z0. Now, except for the Angular X Motion, all the other Motion presets should be Locked.
Press Play and your Camera should travel around the Island;
- In order to start the ride only when we want, first it’s necessary to go into the Animator panel and select the Default Take node. In the Inspector panel change it’s name to “Idle” and select None for it’s Motion. Second, right click the empty space around the Animator state nodes and choose from the dropdown menu: Create State>Empty . Right click the new node, select Make Transition and connect it to the Idle state node. With this new state still selected, in the Inspector panel, rename it to “Go” and choose Default Take for its Motion. If you press Play, the ride will not start automatically anymore;
- The only thing left for us to do is to create an object that will be used as a timed gaze trigger to start our Roller Coaster Ride. First, you need a Reticle so you can point and interact with objects. In the Project tab go through Assets>Cardboard>Prefabs>UI and drag the CardboardReticle prefab into the Head object in the Hierarchy panel.
Head will now have two children: Main Camera and the CardboardReticle;
- Now lets create a cube in our scene, right click the empty space in the Hierarchy panel and choose 3D Object>Cube. This cube will work as a trigger to start our Ride. Next, in the Hierarchy panel, select Main Camera and in the Inspector panel add a Physics Raycaster to it (click Add Component and search for Physics Raycaster);
- Back in the Hierarchy panel right click the empty space and add an EventSystem (UI>Event System). With the EventSystem still selected go to the Inspector panel and add a Gaze Input Module. Click its settings and choose Move Up from the drop down menu so its script doesn’t interfere with the Standalone Input Module that let’s you look around the scene using your mouse in Unity’s Game preview window. We are almost there;
- Finally we need a script to tell our roller coaster when to start rolling. Download this script into your Assets folder (if I remember correctly I used Ryan Zehm’s script from his How to make a 3D Radial Button for Mobile VR in Unity tutorial. Now select the Cube and drag the StartRide script into the Inspector panel. Still in the Inspector panel add an Event Trigger component to it. In the Event Trigger component click Add New Event Type button and choose Pointer Enter, click it again and this time choose Pointer Exit. Click the Plus icon on both and drag Start Ride script into both slots, from the right slot choose StartRide>SetGazedAt. At Pointer Enter tick the box and at Pointer Exit leave it unticked;
- With the object Plane (parent) selected go the Inspector and under its name create a new Tag, type Plane and select this new Tag for it so our script can find it.
Hit Play and you’ll see that you have a gaze reticle that will expand when you look at the cube. If you look at the cube for more than 1 second the ride starts.
The ride has a constant speed because we didn’t animate it in Blender yet, that could be for another tutorial hopefully.
I hope you enjoyed this tutorial and found it useful.