Create a Virtual Reality Game For Google Cardboard

January 7th, 2015

Create a Virtual Reality Game For Google Cardboard

Getting Started
We're going to create a virtual reality game that can run on Android and iOS devices using Unity plugins. Our first game will be quite simple: We'll add an autowalk feature to the Google Cardboard Unity Demo and we'll also add the ability to teleport cubes by looking at them for 2 seconds because not all mobile VR headsets come with the magnetic trigger on the side.

Unity
This is the game engine we'll use to create our game.

Google Cardboard Unity Plugin
Unity plugin created by Google that provides us with scripts for easily adding certain features to our game such as head tracking, stereo rendering, input detection, etc. This plugin will require you have the  Android SDK downloaded.

Character Controller code written in C#
The  character controller asset provided by Unity allows us to easily implement a first-person game but that code is written in JavaScript and the Cardboard Unity plugin code is written in C#. While we can communicate between those two scripts, it'll be easier if everything were in C#. Therefore, we'll use the same character controller code that a Unity user  rewrote in C# except the files provided have been modified to include an autowalk feature.

Creating the game
The first thing we'll do is open up Unity and create a new project. We'll call it firstgame and we'll select the following packages to import:  Skyboxes and  Terrain Assets.

Once Unity opens up we'll import the Google Unity package by going to:  Assests > Import Package > Custom Package and then select the Google Unity Plugin you downloaded and click import. In the project window you should see a Cardboard folder within an Assets folder. Open the Cardboard folder and then open the DemoScene folder. Double click on DemoScene and your project should look like this:



In the Hierarchy window, drop down  CardboardMain > Head > Main Camera and make sure Main Camera is selected. Now if you run your game you can modify the Stereo Multiplier option in real-time in the Inspector window. Stereo Multiplier affects the stereopsis for your game; it controls how  cross-eyed the stereo rig should be.

If you can see double images in your game later when playing then one way to possibly fix the issue is to change the value of Stereo Multiplier. (Other reasons you could be seeing double is because of the distance between the lenses on your headset or the distance from your phone to the lenses needs to be changed).



With the MainCamera in the Hierarchy still selected, in the Inspector window click Add Component and select  Rendering > Skybox. Now click on the little circle next to None (Material) and some skyboxes should show up. Double click any of them (I'm going to choose Eerie Skybox).

Now select the ground plane in your game window and in the Inspector window select  Mesh Renderer > Materials and select the little circle near  Element 0. Then choose the Grass Hill material and run the game.

Hold the Alt key to look around the scene.





Walking around in your game
In your Project window click on the  Assets folder and right click in the window and select  Import New Asset, then select the  CharacterMotor.cs and the FPSInputController.cs files that were provided in the zip file above.

Now select  Head in the  Hierarchy window and drag the FPSInputController script into the  Inspector window (Character Controller and Character Motor (script) should automatically be added). Then delete  CardboardGUI from the Hierarchy window. So far it should look like this:



Now if you run your game you should be able to walk around using the WASD keys.



Autowalk feature
Most games currently being developed for mobile virtual reality allow the player to walk around by using an autowalk feature if they don't have a controller or keyboard. It works the following way: if the player looks at some object for about 2 to 3 seconds, autowalk is enabled and they walk in the direction they are facing and if they look at the same object again, then autowalk is disabled. This is what we'll implement in our game.

Select  Game Object > 3D Object > Sphere and rename the newly created sphere to Autowalk.

With the Autowalk sphere still selected, in the Inspector window click Add Component and select  New Script and name it Autowalk and choose CSharp as the language. Then double click this new file, which should open up in MonoDevelop, and paste this code into that file:

Autowalk.cs

  using UnityEngine;
  using System.Collections;
   
  public class Autowalk : MonoBehaviour {
   
  private CardboardHead head;
  private Vector3 startingPosition;
  private float delay = 0.0f;
   
  void Start() {
  head = Camera.main.GetComponent<StereoController>().Head;
  startingPosition = transform.localPosition;
  }
   
  void Update() {
  RaycastHit hit;
  bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
  // if looking at object for 2 seconds, enable/disable autowalk
  if (isLookedAt && Time.time>delay) {
  GameObject FPSController = GameObject.Find("Head");
  FPSInputController autowalk = FPSController.GetComponent<FPSInputController>();
  autowalk.checkAutoWalk = !autowalk.checkAutoWalk;
  delay = Time.time + 2.0f;
  }
  // currently looking at object
  else if (isLookedAt) {
  GetComponent<Renderer>().material.color = Color.yellow;
  }
  // not looking at object
  else if (!isLookedAt) {
  GetComponent<Renderer>().material.color = Color.red;
  delay = Time.time + 2.0f;
  }
  }
   
  }
view raw Autowalk.cs hosted with ❤ by  GitHub
This code sets the color of the Autowalk sphere to red when it's not being looked at and it sets the  delay variable to 2 seconds into the future. If the player looks at the sphere and 2 seconds haven't elapsed, then the sphere simply becomes yellow and stays yellow to signify for the player they are looking at it. Once 2 seconds have passed, then we want our autowalk feature to start.

Now we'll just place the Autowalk sphere high up in the scene so you can look at it from anywhere. Scale it up to 3 in all directions and place it above the point light in the scene.



Now you should be able to look at the sphere and autowalk should enable or disable depending on if you're currently walking or not.

Teleporting cubes without magnetic input
Because not all mobile VR headsets come with a magnetic trigger on the side, we'll add the ability to look at cubes for 2 seconds to make them teleport rather than solely relying on the magnetic input.

If you click on the Cube in the  Hierarchy window you should see a Teleport script attached as a component. Double click this script and it should open up in MonoDevelop. Copy and paste the following code into that file.

Teleport.cs

  using UnityEngine;
  using System.Collections;
   
  [RequireComponent(typeof(Collider))]
  public class Teleport : MonoBehaviour {
   
  private CardboardHead head;
  private Vector3 startingPosition;
  private float delay = 0.0f;
   
  void Start() {
  head = Camera.main.GetComponent<StereoController>().Head;
  startingPosition = transform.localPosition;
  }
   
  void Update() {
  RaycastHit hit;
  bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
  GetComponent<Renderer>().material.color = isLookedAt ? Color.green : Color.red;
  if (!isLookedAt) { delay = Time.time + 2.0f; }
  if ((Cardboard.SDK.CardboardTriggered && isLookedAt) || (isLookedAt && Time.time>delay)) {
  // Teleport randomly
  Vector3 direction = Random.onUnitSphere;
  direction.y = Mathf.Clamp(direction.y, 0.5f, 1f);
  float distance = 2 * Random.value + 1.5f;
  transform.localPosition = direction * distance;
  }
  }
   
  }
view raw Teleport.cs hosted with ❤ by  GitHub
Building for mobile devices
We're now ready to run our game on a mobile device. Click  File > Build Settings

1. Click Android and press  Switch Platform.
2. Click the  Add Current button to add the current scene.
3. Click the  Player Settings button and in  Resolution and Presentation > Default Orientation select Landscape Left.
4. In  Other Settings provide your game with a  Bundle Identifier such as com.first.game

After building is complete you should have an apk file in your game folder that you can run on your device.



内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值