How to Move a Player Inside a Room (Unity VR Game Tutorial)

Does your game have rooms you want the player to enter? Let’s learn how to implement a game like that in Unity virtual reality! A free tutorial by Mammoth Interactive.

In this tutorial:

Setup

To follow along, you’re going to need a Unity virtual reality project open with a basic room set up for the player to enter through its door. If you need help setting up such a Unity project, go to our previous post Building a Castle in a Unity VR Game.

We’re using Unity v5.4.3f1. If you’re using a different version of Unity and find an incompatibility, drop us a comment down below with your question.

Step 1: Write the Player Logic

To make the player move inside the castle, we’ll need to write custom C# code. So, in your Unity project, open the Player script, Player.cs.

In the Player class, declare a Vector3 variable named targetPosition. This variable will hold the location to where we want the Player to move.

private Vector3 targetPosition;

In the Start method, use the following code to save Player’s current position in targetPosition by getting the Player’s Position property in its Transform component.

void Start () {
    targetPosition = transform.position;
}

In the Player class, declare the variable speed, which we will use to make Player move. Set the value of speed to 0.5f for now.

public float speed = 0.5f;

The Lerp() Method

In the Update method, call the Lerp method to move Player from transform.position to targetPosition in Time.deltaTime * speed.

Making a Lerp is a linear interpolation. It is like we are making an animation that is going from Door’s current position to targetPosition.

  • The first parameter of Lerp, transform.position, is Player’s source position.
  • The second parameter, targetPosition, is Player’s destination position.
  • Player will move from transform.position to targetPosition in Time.deltaTime * speed.

void Update () {
   Raycast hit;
   if (Physics.Raycast(transform.position, transform.forward, out     hit)) {
     if (hit.transform.GetComponent () != null) {
       Debug.Log ("Looked at door button!");
       hit.transform.GetComponent ().OnLook ();
     }
   }
   transform.position = Vector3.Lerp (transform.position,         targetPosition, Time.deltaTime * speed);
}

Step 2: Change the Door Opening Speed

You can similarly use speed to change the speed at which your room’s door opens. If you don’t have a door, check out our last tutorial.

Open Door.cs. In the Door class, declare the speed variable. Give speed the value 3f.

public float speed = 3f;

In the Update method, where you update the door’s position, multiply Time.deltaTime by speed.

void Update () {
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * speed);
}

colorful arrows pointing inward

Step 3: Define the Player’s Position Inside the Room

Save the script, and open Player.cs. Here we need to define the position where Player is in the castle. In the Player class, declare the variable castlePosition.

public Vector3 castlePosition;

To set the value of castlePosition, we need to know the Position values of Player when Player is inside the castle. We can get these Position values from the Inspector.

Save the script, and open Unity. Select Player. Change its Y Position to 1.6, which represents the height of a human.

changing a player's height
This example is from the same game as previous tutorials.

Move Player inside the castle. You can see that when Player is in the center of the castle, Player’s Z position is about 13.

moving a player inside a room in unity
We are getting the position of the player when they are inside the room.

Now we have the player’s position when they are in the castle.

Move Player back outside the castle to its original position by undoing the move. In the Player (Script) component in the Inspector, change Castle Position to 0 1.6 13.

Step 4: Move the Player Inside the Room

We will use the Castle Position property to set Player movement. The player will move when they look at Button.

Open Player.cs. Below the Update method, declare the method MoveToCastle.

private void MoveToCastle () {

}

Call MoveToCastle in the Update method, when the player looks at the door button.

void Update () {
Raycast hit;
if (Physics.Raycast(transform.position, transform.forward, out hit)) {
if (hit.transform.getComponent<DoorButton> () != null) {
Debug.Log ("Looked at door button!");
hit.transform.GetComponent<DoorButton> () .OnLook ();
MoveToCastle ();
}
}

transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * speed);
}

To move Player inside the castle, set the value of targetPosition to castlePosition in the MoveToCastle method.

private void MoveToCastle () {
targetPosition = castlePosition;
}

Let’s test the code. Save the script, and open Unity. Press Play. Look at Button. Door will lower, and you will move inside the castle.

That means success! We’ll continue this game in the next post… unless you object…. What tutorials do you want to see next? Drop a comment below.

Review

—Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova

Liked this tutorial? Get a free game development video course!

Leave a Reply

Your email address will not be published. Required fields are marked *