Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / game / Unity

Day 62 of 100 Days of VR: Implementing Movement in Google Daydream

0.00/5 (No votes)
27 May 2018CPOL3 min read 1.3K  
How to implement movement in Google Daydream

Introduction

In the previous post, we gained a basic understanding of how to create scripts to use the Daydream controller.

Today, we’re going to work on creating some gameplay mechanics we can start playing with. Specifically, we’re going to look at how we can use the touch controls to implement movement.

Let’s get started.

Moving Around with the Daydream Controller

In the previous series about the Google Cardboard, we implemented a couple of ways for us to travel around the game. Today, we’re going to look at another way that’s exclusive to the Google Daydream.

Before, we had only one input for the cardboard, but now with the daydream controller, we have more, specifically, we have access to the touchpad which allows us to detect swiping!

How are we going to implement movement with this?

Goal: We’re going to use the touchpad position to figure out which direction we should move our character.

Let’s get started.

  1. In Player, create a new script component called TouchpadMovement
  2. We can remove the PlayerInput component

In TouchpadMovement, we’re going to write code that will let us move our character around in the game based off where they are touching the touchpad. The further their fingers are in the touchpad from the center, the faster we should be able to move our character. Here’s what it looks like:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchpadMovement : MonoBehaviour
{
    private float _speedSlowDown;
    private Camera _mainCamera;

    void Start()
    {
        _speedSlowDown = 0.1f;
        _mainCamera = Camera.main;
    }

	void Update ()
	{
        _mainCamera.transform.position += new Vector3(0, 1, 0);
        if (GvrControllerInput.IsTouching)
        {
	        Vector3 touchPos = GvrControllerInput.TouchPos;
            Vector3 movementVector = new Vector3
               (touchPos.x - 0.5f, 0, touchPos.y - 0.5f); //adjust for center to be at 0.5, 0.5
            transform.Translate(movementVector.x * _speedSlowDown, 
               0, -movementVector.z * _speedSlowDown); // negative to adjust for the vector speed
        }
    }
}

Variables Introduced

Here are the global fields that we’re going to use:

  • private float _speedSlowDown – Used to adjust the speed of how fast our character is going
  • private Camera _mainCamera – References our main camera for us to access

So far, nothing too complicated… yet!

Walking Through the Code

Like our swiping code that we have seen before, we already know how to read the positions of where the players are touching the touchpad on the daydream controller. Now we just need to use that information.

  1. We know that the top left corner is (0, 0) and that the center is (0.5, 0.5) so to know which direction the player is touching, we just subtract (0.5, 0.5) from wherever the player is touching. From there, we would have the vector of which direction we should move the player.

    Let’s think about the values for each X and Y values individually.

    Moving Left (X Value)

    If the player wants to go left, their position would be (0, 0.5). If we subtract that with (0.5, 0.5), we would get a vector of (-0.5, 0).

    This makes sense, we want to go left so our X would be -0.5.

    Moving Up (Y Value)

    The Y value, however, is a bit different, because our starting value is at the top at 0.

    Let’s say we want to move forward. The Touch position vector would be (0.5, 0). If we subtract that with (0.5, 0.5), we would be at (0, -0.5).

    Does that seem strange to you? It should, we should expect to see a positive number, not a negative one. So later on, we need to inverse the sign of our Y value so that we can get the correct position.

  2. After we get the position to go to, we would need to translate the Vector2 to be a Vector3. In this instance, the X value would stay the same, however in a 3D plane, the Y value is vertical movement. We don’t want to rise our character, we just want to move it forward. The specific 3D axis that controls that is the Z value.

With that, we now have a basic implementation of movement!

Conclusion

At this point, if you played the game, everything will work great, we can move in the direction that we’re moving our touchpad.

However, let’s play with an interesting problem. What happens if we look to the left and slide our touchpad forward?

In our perspective, we would move right (our original forward). This is fine if we’re just looking to the left.

However, what if in our game, we turn our whole body around? Now our game is broken!

In the next post, we’re going to explore more about how we can fix this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)