Implementing a Button | Unity Tutorial

In virtual reality (VR) games, one of the few interactions a player can make is press a button by looking at it. In this tutorial, we will implement the button we made in our previous Unity tutorial.

If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

There are two ways to interact with the button. Both ways involve looking (the looking logic). We need to understand how to identify the button. To do this, we will perform raycasting.

To draw an image, Main Camera throws rays in multiple directions to find pixels to draw. We will use the ray that shoots forward in the direction the camera is facing. If the ray flying from the camera touches an object, we will identify the object.

Rename Main Camera “Player” because the player will look through the lens of the camera. We can treat the camera as the eyes of the player. Create a C# script in Assets. Name the script “Player”. Drag and drop the Player script to “Player” in the Hierarchy.

Double-click on the Player script to open it. Type the following code in the Update method in Player.cs. This code declares the local variable hit of type RaycastHit.

public class Player : MonoBehaviour {
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Raycast hit;
}
}

hit is a local variable because we declared it in one function. You can only call a local variable within its function. If you know you are only going to use a variable in one function, declare it as a local variable.

If you declare a variable in a class outside of a function, you can use the variable throughout the functions in the class. These variables are global.

If you hover over “RaycastHit”, you can read that if your raycast hits an object, the object will be stored in hit, along with other details.

Create the following if block, which calls the Raycast method from the Physics class.

// Update is called once per frame
void Update () {
Raycast hit;

if (Physics.Raycast()) {

}
}

We need to pass three parameters to perform the raycast:

1. We need to pass the origin of the raycast. We want the ray to come from the exact position of the camera. transform.position is the position of Player.

// Update is called once per frame
void Update () {
Raycast hit;

if (Physics.Raycast(transform.position)) {

}
}

2. We need to pass the direction of the ray. transform.forward refers to the direction of the blue arrow. transform.forward always points forward even when the camera rotates.

// Update is called once per frame
void Update () {
Raycast hit;

if (Physics.Raycast(transform.position, transform.forward)) {

}
}

3. We need to pass where we will save the hit information if there is a hit. We will store the information in the hit variable.

// Update is called once per frame
void Update () {
Raycast hit;

if (Physics.Raycast(transform.position, transform.forward, out hit)) {

}
}

The if block’s condition will return true when the raycast hits something. In this case, we will use the hit variable. The condition will return false when the raycast does not hit anything. hit will not store a value, so we will not use it.

If the condition returns true, use the following code to print the name of the object the raycast hit.

// Update is called once per frame
void Update () {
Raycast hit;

if (Physics.Raycast(transform.position, transform.forward)) {
Debug.Log (hit.transform.name);
}
}

Save the script, and open Unity. Press Play. Press Alt/Option, and move the cursor until the crosshair is on top of Button. The console will print the message “Button” constantly because every time the Update method is called, it sees that we are looking at the button, and it prints the message.

Save your project. Want to learn more about buttons? Check out our Unity3D course, where you build 30 virtual reality games!

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com