In augmented reality, the fusion of the virtual and physical worlds has sparked a new dimension of interactive experiences. One fundamental aspect of AR application development is the ability to select, scale, and rotate virtual objects within the real-world environment. This demo shows how you can do that.
XR interaction toolkit in Unity represents a framework designed to facilitate the creation of interactive and immersive experiences within extended reality environments. The XR interaction toolkit serves as a foundational layer, helping you integrate user interactions, such as grabbing objects, manipulating them, and engaging with the virtual world naturally and intuitively.
This toolkit abstracts the complexities of XR input systems, offering a unified approach for handling various input devices like controllers, hand tracking, and gestures.
We will select, scale, and rotate a virtual object using the XR interaction toolkit. First, create a Unity project and set up the AR settings.
The XR interaction toolkit provides a collection of components, scripts, and utilities that streamline the process of implementing interactive features. These include object selection, movement, rotation, scaling, and other user-driven actions that enhance engagement and immersion.
Download the XR interaction toolkit by going to Window > Package Manager. Search for the XR interaction toolkit in the Unity registry and install it.
In the hierarchy tab, Right-click > XR > Interaction Manager also, Right-click > XR > AR Placement Interactable.
The AR gesture interactor component bridges augmented reality experiences and user interactions, allowing you to integrate gesture-based input into your applications.
This component is designed to interpret and respond to various user gestures and interactions within the AR environment.
Go to your main camera in XR origin and add the AR gesture interactor component.
Create the GameObject, that you want to apply the manipulations on.
Go to the hierarchy tab and add a cube GameObject. Give the object a color of your choice. Select the GameObject and Right-click > Create Empty. Name the GameObject, "selection." Repeat this step and name the next GameObject "rotator."
Create
Note: This material will be used when your object is selected.
When the material is created, drag it to the selection GameObject.
Once your GameObject is created, you will have to add certain components on it to scale and rotate the object.
Select your AR cube and add the following components:
The AR selection interactor is a component that enables users to interact with virtual objects by selecting them. This interaction allows users to pick up, manipulate, and engage with digital elements within the AR scene. It facilitates actions such as grabbing, dragging, and releasing virtual objects, creating a tangible connection between the user and the AR environment.
Add your "selection" GameObject, next to the "Selection Visualisation" tab.
The AR translation interactable refers to the ability of virtual objects to be moved or translated within the AR space. Users can directly manipulate the position of objects by dragging or pushing them, altering their location in relation to the real world. This interaction allows users to arrange dynamically and reposition digital content.
The AR scale interactable feature enables users to adjust the size of virtual objects in the AR environment. Using gestures like pinching or stretching, users can change the scale of objects, making them larger or smaller. This interaction is useful for accommodating user preferences and spatial considerations within the AR scene.
Change the minimum scale to the least size you want your object to have. Likewise, change the max scale that you want your object to scale to at the max.
The AR rotation interactable allows users to rotate virtual objects around their axes within the AR world. By employing gestures like twisting or turning, users can control the orientation and angle of objects. This interaction adds a layer of realism to the experience and empowers users to fine-tune the alignment of virtual content.
Go to the placement interactable in the hierarchy tab. Add your cube object next to the "Placement Prefab" field.
The AR placement interactable focuses on the precise positioning of virtual objects within the physical environment. It allows users to carefully place digital content in specific locations using gestures or controls. This feature is crucial for integrating virtual elements seamlessly into real-world scenes.
Save the scene. Connect your Android or iOS device. Go to File > Build and Run.
In the ever-evolving landscape of augmented reality, the ability to manipulate virtual objects within the real world is a cornerstone of user engagement and interaction. Through Unity and the AR foundation, you can implement object selection, scaling, and rotation. As we look ahead, the convergence of technology and creativity will undoubtedly continue to reshape the boundaries of what is possible in AR.
Free Resources