FREELessons: 8Length: 41 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.5 Interaction: Pulling the Trigger

In this lesson, we'll modify the code that executes when the user pulls the trigger on their Cardboard viewer. Whenever the user finds the cube object, we'll make some changes to the scene and present a new cube.

2.5 Interaction: Pulling the Trigger

Hello. Welcome back to the Tuts+ VR for Google Cardboard Android Course. My name's Sue Smith. Last time we added to the code that executes when the user looks an object and the same. This time we will look at the code that executes when the user pulls the trigger in the cardboard viewer. Some viewers don't actually have trigger buttons on them and if you have one like that you can simulate the effect of pulling the trigger by tapping the device screen. If we look at the on cardboard trigger method, we see that it first checks whether or not the user is looking at the cube using the helper method we saw last time. If the user is looking at the cube their score is increased and the cube is moved to a new location. If they are not looking at the cube, the app simply displays a message in the overlay view telling them to keep looking. Let's acd to the code to executes when the user is looking at the object. Earlier we changed the floor to ceiling by altering its depth. Now let's swap between the two each time the user gets a point And now we need to apply a transformation in the floor. Let's use the same code that the app already uses in onSurfaceCreated. And let's run the app to see what happens when we get a point. Let's try another change now. Earlier we looked at how we could display five sides of the cube instead of six. Let's adjust the trigger code to display a random number of sides each time the cube moves. First go back to the world layout data and uncomment out the sites that we had earlier. And if you've deleted any code that you want to retrieve, you can always get it from the original sample again. Now in the main activity let's add an instance variable for the number of visible sides on the cube. Initially we'll set it to six and we'll also use a random variable to choose an integer within a particular range. Now in drop cube, let's update the code we changed earlier to use this new variable. And then the trigger method, let's choose a number of sites at random but keep it within a range that will ensure it remains easily visible. You may have noticed that when the cube is close to the user sometimes the app doesn't recognize that they're looking at it. Let's make a slight adjustment to the app logic to give the user a bit more control. We'll move the call to height object outside the conditional so that the object will move any time the user pulls the trigger regardless of whether they are looking at the object.. But their score will only increase if they push the trigger while looking at it. Let's run the app to see these changes in action. The cube appears with a random number of sides each time and it moves any time we place the trigger. Carry on experimenting with the app by making more changes of your own.

Back to the top