Join Michael House for an in-depth discussion in this video Pick up and move objects, part of Virtual Reality Overview for Developers.
- [Instructor] The more interaction we can add in VR, the better the user experience. I'm going to show you how to pick up and move objects in the VR world. We're going to use raycasting to detect objects and then respond to the user's button press by attaching the object to the front of the camera. Here we have a scene that has a table and a couple of objects on it. I've already started an Object Picker script. Let's go ahead and open that up and we'll fill in the update method. This script is going to test for input and if it receives input, it's going to check to see if it's over an object it can pick up.
If the user's looking at an object that can be picked up, the object will be attached to the front of the camera. And on the next button press, the object will be dropped. I've already written a few methods to make this an easier script to write. This is an object that casts a ray into the scene. If a game object is hit, it's returned, otherwise null is returned. This is a method that tests to see if a button has just been pressed. This is a method that will pick up the passed in gameobject and this is a method that will drop any current object that's been picked up.
Let's see if we can use those in our update method to create the functionality we need. First we want to see if the button has been pressed. If we don't have a picked object, meaning our picked object equals null, and we want to pick up any object returned by the cast ray method. The pickup method will ignore nulls so it's fine if we're not currently looking at an object. If the picked object is not null, else, we're going to drop whatever picked up object we have.
Finally, even when the button is not pressed, if we have a picked up object, meaning picked object does not equal null, we want to make sure it's attached to the front of the camera. So we're going to set its position, pickedObject, transform, position, equals this.transform.position plus this.transform.forward times the offset.
The offset is set when an object is picked up. It's the distance between the camera and the object at that time. This ensures that the object is attached with the same distance it was picked up from. As you can see since we're using this transform position, we're assuming that this script is going to be attached to the camera. Let's save our script and go back to Unity to see how this looks. Now that we've completed our ObjectPicker script, we need to attach it to the main camera.
Here we can verify that it's been attached. We'll save our scene and hit play. Now we see our objects and in order to simulate the taps, we'll need to play on the mobile device. One more thing before we continue. Let's click on our forward object and set its layer to ignore ray cast and we'll do the same for the table. This ensures that neither of those objects can be picked up by our script. We'll save and File Build Run.
Build and Run, creating output directory, and name our IPK ObjectPicker. Here we have our scene, our three objects, and we can tap the screen to pick up an object and move it around. And that began to drop it. One feature you may want to add to this project, directional pointer so user knows where they're clicking.
Picking up and moving objects in VR has plenty of potential. Using only the rotation of the device and a button click, we can provide this feature. This goes to show that even with limited inputs, we can provide a fairly rich user experience.
- The state of virtual reality
- VR user interfaces
- Setting up a development environment
- Setting up a project
- Creating scripts
- Adding a custom skybox
- Mobile device configuration
- VR cameras
- Interacting with the VR environment
- Spatial audio