Blog.

Blog03 - Mastering Joystick Controls: Enhancing AR Interactions in Unity

Tomas Ondrejka

Introduction

Augmented Reality (AR) has become a pivotal technology in today's gaming and application development ecosystem. However, intuitive interaction within the AR space remains a challenge. One popular solution to this is the incorporation of on-screen joysticks. In this blog, we'll walk through integrating joystick controls in Unity AR for seamless object movement and rotation.

Setting Up the Joysticks

  1. Downloading the Joystick Pack:
    1. Navigate to the Unity Asset Store.
    2. Search for the joystick pack and download it.
    3. Once downloaded, import the joystick pack into your project.
  2. Integrating the Joysticks in the Scene: Drag and drop two FixedJoysticks from the imported package into your scene, positioning one on the left and the other on the right. Adjust the properties, such as size and position, to best fit your scene's requirements.
  3. Script Creation: Create a new script named ARObjectController. Within this script, declare:
public FixedJoystick LeftJoystick;
public FixedJoystick RightJoystick;
  1. Attaching the Script: Attach the ARObjectController script to a parent GameObject in your scene. Using the inspector, drag and drop the left and right joystick objects from the hierarchy into their corresponding public variables in the script.

XObject Movement and Rotation:

  1. Object Placement: AR works by overlaying virtual objects onto the real world, often anchored to detected surfaces. Using a raycast, we can detect these surfaces:
if (raycastManager.Raycast(new Vector2(0.5f, 0.5f), hits,
TrackableType.Planes))
{
    ARRaycastHit hit = hits[0];
    questPosition = hit.pose.position;
    quest =
Instantiate(Resources.Load(selectQuestHandler.placeObject.objectToPlace),
questPosition, hit.pose.rotation);
}

Note: Ensure that raycastManager, selectQuestHandler, and quest are appropriately initialized in your script.

  1. Setting up the Listener: For real-time object manipulation, monitor the joystick movements in the Update method of your ARObjectController script.

  2. Object Manipulation: With the listener in place, translate the joystick's direction into object movement and rotation:

void Update()
{
    float moveX = LeftJoystick.Horizontal;
    float moveZ = LeftJoystick.Vertical;
    Vector3 moveVector = new Vector3(moveX, 0, moveZ) * (moveSpeed *
Time.deltaTime);
    quest.GameObject().transform.Translate(moveVector, Space.World);

    float rotateY = RightJoystick.Horizontal;
    Vector3 rotateVector = new Vector3(0, rotateY, 0) * (moveSpeed *
Time.deltaTime * 100);
    quest.GameObject().transform.Rotate(rotateVector, Space.World);
}

Here, we've restricted rotation to the Y-axis, allowing for a natural spin. The multiplication by 100 provides a more significant rotation effect for smaller joystick movements. Ensure you've defined the moveSpeed variable as it determines how fast the object moves or rotates.

Conclusion

Integrating joystick controls in Unity AR applications offers an intuitive way for users to interact with virtual objects. Whether you're developing games or AR tools, this method provides an engaging experience. With the steps outlined above, you're well on your way to creating more interactive and dynamic AR scenes in Unity.