Build Your First VR App(unity for oculus)

Stella981
• 阅读 1019

This tutorial helps you build your first VR app in Unity. It’s a basic app, which introduces primary Unity concepts such as 3D objects, components, and build settings. It does not use Oculus Integration package as the objective of this tutorial is to get you started with Unity’s basic concepts and interface. At the end, you’ll have a VR app ready to run on your computer.

What’s the app about?

It’s a simple game! The scene contains a play area surrounded by four walls and a ball that acts as a player. The objective of the game is to keep the ball rolling without colliding with the walls. If it collides with either of the walls, the wall color should change and a text should display on the screen indicating the collision. For input, you need to use your keyboard or Unity-compatible joysticks.

This is what we’ll build:

Prerequisites

Before you begin with this tutorial, make sure that you’ve set up your development environment and performed the necessary settings.

  1. Set up the Oculus device for development and enable it for testing.
  2. Install Unity Editor.
  3. Create a new project.
  4. Configure build settings.
  5. Enable VR support.

Basic Concepts

This section introduces fundamentals for creating app or gameplay mechanics. We’ve limited the explanation to core workflow concepts that come handy in building this VR app. To learn more about Unity’s concepts and workflows in detail, go to Unity User Manual.

  • Scene is a container that holds a variety of game objects.
  • Game Objects are fundamental objects that represent characters, props, lights, camera, or special effects. In this app, we’ll use 3D objects that consist of primitive shapes such as plane, cube, and sphere.
  • Components define the behavior of the game object. Mainly, the Transform component determines the position, rotation, and scale of each game object and the values are represented in form of X, Y, and Z coordinates for each property. By default, the position is set to (0,0,0), which is also known as the origin point from where all the coordinate calculations take place in the scene.
  • Material adds texture and color to any object. In this app, we’ll limit the usage of materials to color the objects and will not go in other technical details.

Build App

Step 1. Create material to add colors to game objects.

One of the basic needs in any app design is to add colors and textures. Materials let you define a variety of look-n-feel effects such as colors, shaders, textures, and many more. In this app, we’ll limit the usage of materials to add colors.

  1. In the Project view, in the Assets folder, create a new folder to hold materials for different game objects of the app, rename it to Materials, and double-click to open the folder.

  2. In the menu, go to Assets > Create > Materials and rename the material to floor-color.

  3. In the Inspector view, under Main Maps, click the Albedo color field to open a color picker and change the color of your choice. In this app, we’ll use RGB values of (3,32,70).

    Build Your First VR App(unity for oculus)

  4. Repeat step 2 through step 4 to create materials for walls, player ball, and the change in wall color upon collision. Rename them to wall-color, ball-color, and after-collision and set RGB values to (255,255,255), (240,240,0), and (241,107,8) respectively.

    Build Your First VR App(unity for oculus)

Step 2. Build the floor, player ball, and four walls.

The game objects of this app are floor, ball, and four walls and we’ll use stock Unity plane, sphere, and cubes to build them.

Floor:

  1. In the menu, go to GameObject > 3D Object > Plane.

  2. In the Hierarchy view, rename it to floor.

  3. In the Inspector view, under Transform, verify the position is set to origin, i.e., (0,0,0). If not, reset it to (0,0,0).

  4. Under Transform, set the scale to (2,1,2) to enlarge the floor. The plane object is flat and does not have any volume. Therefore, by default, the Y value is set to one.

  5. Drag and drop the floor-color material on the plane to add color.

    Build Your First VR App(unity for oculus)

Player ball:

  1. In the menu, go to GameObject > 3D Object > Sphere.

  2. In the Hierarchy view, rename it to player-ball.

  3. From the Materials folder, drag and drop ball-color on the sphere.

  4. In the Inspector view, under Transform, ensure the position is set to (0,0,0), and set the Y value to 0.5 to let the ball rest on the floor, i.e., (0,0.5,0).

    Build Your First VR App(unity for oculus)

Four walls:

  1. In the menu, go to GameObject > 3D Object > Cube.

  2. From the Materials folder, drag and drop wall-color on the cube.

  3. In the Inspector view, under Transform, reset the position to (0,0,0), and set the scale to (0.5,2,20.5) to stretch the wall so that it fits the floor edges neatly.

  4. In the Hierarchy view, right-click the cube, and do the following:

    a. Rename the cube to first-wall.

    b. Duplicate the cube three times to add three more walls and rename each of them to second-wall, third-wall, and fourth-wall, respectively.

  5. In the Hierarchy view, select third-wall, and in the Inspector view, under Transform, set the rotation to (0,90,0). Repeat the step for fourth-wall.

  6. In the Hierarchy view, select first-wall, and in the Inspector view, under Transform, enter the following values to reposition the wall to surround the floor from all directions. Repeat this step for the rest of the walls.

    first-wall to (-10,0,0)

    second-wall to (10,0,0)

    third-wall to (0,0,10)

    fourth-wall to (0,0,-10)

    Build Your First VR App(unity for oculus)

Step 3: Adjust camera and lighting.

Adjust the main camera and lighting to get a better view of the scene.

  1. In the Hierarchy view, select Main Camera.
  2. In the Inspector view, under Transform, set the position to (0,10,-20) and rotation to (20,0,0).
  3. In the Hierarchy view, select Directional Light.
  4. In the Inspector view, under Transform, set the rotation to (50,60,0).

Step 4: Add movement to the player ball.

  1. In the Hierarchy view, select player-ball.

  2. In the Inspector view, do the following:

    a. Click Add Component > Physics > Rigidbody.

    Build Your First VR App(unity for oculus)

    b. Click Add Component > New Script, set the name to PlayerController, and click Create and Add.

    c. Click the gear icon next to the PlayerController script and click Edit Script to open it in the code editor.

    Build Your First VR App(unity for oculus)

    d. Replace the sample code with the following to grab the input from the keyboard and add forces to move the ball.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { // Appears in the Inspector view from where you can set the speed public float speed; // Rigidbody variable to hold the player ball's rigidbody instance private Rigidbody rb; // Called before the first frame update void Start() { // Assigns the player ball's rigidbody instance to the variable rb = GetComponent<Rigidbody>(); } // Called once per frame private void Update() { // The float variables, moveHorizontal and moveVertical, holds the value of the virtual axes, X and Z. // It records input from the keyboard. float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); // Vector3 variable, movement, holds 3D positions of the player ball in form of X, Y, and Z axes in the space. Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); // Adds force to the player ball to move around. rb.AddForce(movement * speed * Time.deltaTime); } }

  1. In the Inspector view, under PlayerController script, in Speed, enter the speed value. For example, 500. This variable is the same speed variable that we have declared in the PlayerController script in step 4.2.d.

    Build Your First VR App(unity for oculus)

  2. Click the Play button from the top to preview your app. Press the arrow keys on your keyboard to roll the ball around.

Step 5. Add text.

  1. On the menu, go to Game Object > UI > Text.
  2. In the Hierarchy view, select Text, and rename it to message.
  3. In the Inspector view, under Rect Transform, reposition the text. You can also change the font color and font size, if needed.

Step 6. Change the wall color and display text when the ball enters or exits collision.

The collision occurs when the ball hits the wall. You can add effects to highlight the collision. For example, when the ball collides with the wall, the wall color changes and displays text, Ouch!, on the screen. On the other hand, when the ball is back to rolling, the wall color changes to the original color and displays text, Keep Rolling..., on the screen.

  1. In the Hierarchy, select first-wall.

  2. In the Inspector view, click Add Component > New Script, set the name to ColorController, and click Create and Add.

  3. Click the gear icon next to the ColorController script and click Edit Script to open it in the code editor.

  4. Replace the sample code with the following code.

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ColorController : MonoBehaviour { public Material[] wallMaterial; Renderer rend; // Appears in the Inspector view from where you can assign the textbox public Text displayText; // Start is called before the first frame update void Start() { // Assigns the component's renderer instance rend = GetComponent<Renderer>(); rend.enabled = true; displayText.text = ""; } // Called when the ball collides with the wall private void OnCollisionEnter(Collision col) { // Checks if the player ball has collided with the wall. if (col.gameObject.name == "player-ball") { displayText.text = "Ouch!"; rend.sharedMaterial = wallMaterial[0]; } } // It is called when the ball moves away from the wall private void OnCollisionExit(Collision col) { if (col.gameObject.name == "player-ball") { rend.sharedMaterial = wallMaterial[1]; displayText.text = "Keep Rolling..."; } } }

  5. In the Hierarchy view, select first-wall.

  6. In the Inspector view, under ColorController script, do the following:

    a. Expand Wall Material, in Size, enter 2, and in Element 0 and Element 1, drag and drop after-collision and wall-color, respectively.

    b. Click the gear icon next to Display Text and select message. Display Text is the same field that we have declared in the ColorController.cs script in step 6.4.

    Build Your First VR App(unity for oculus)

  7. In the Hierarchy view, select second-wall.

  8. In Inspector view, click Add Components > Scripts, select ColorController from the list, and repeat step 6.

  9. Repeat step 7 and step 8 for the remaining walls.

  10. Click the Play button from the top to preview your app. Press the arrow keys on your keyboard to roll the ball around.

Step 7. Build and run your app.

Depending on the target platform you’ve selected in build settings, Unity builds either .apk file for Android or .exe file for Windows. Since we have not used the Oculus Integration package, the app may not run with Oculus Touch controllers.

  1. Save your scene.
  2. Go to File > Build and Run.
  3. Double-click the file to run the app on your computer. Use your keyboard or Unity-compatible joysticks for input.
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
Unity横屏
Android下发现Unity里面的Player设置,并不能完全有效,比如打开了自动旋转,启动的时候还是会横屏,修改XML添加以下代码<applicationandroid:icon"@drawable/ic\_launcher"                    android:label"@string/app\_name"
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这