XR Step-by-Step! XR Game: Create and Shoot in your Meta Quest XR project in Unity 2022!

Virtual and Augmented Reality together are now referred to as XR or “Extended-Reality”. You’re in the right place at the right time to build something amazing in XR!

XR applications can be created in Unity for many devices, our focus will be on Microsoft Hololens and the Meta Quest 2 – in this blog post series we will specifically focus on Meta Quest 2 in the latest Unity 2022.

So you want to make an XR game? Me too! Let’s start by creating a game object whenever we click our trigger and then make it shoot out from our controller 🙂

Here’s my XR Step-by-Step blog series to help you create your XR experiences!

Unity 2022
1) Installing the Tools you need for XR development == Unity 2022
2) Setting up your Meta Quest XR project in Unity 2022!

3) Helpful Asset Packages & Libraries for Unity 2022
4) Organizing your Unity Projects

5) Setting up Mixed-Reality Passthrough for your Meta Quest XR project in Unity 2022!

6) Setting up Touch Controller for your Meta Quest XR project in Unity 2022!
7) XR Game: Create and Shoot in your Meta Quest XR project in Unity 2022!

Unity 2021

1) Installing the Tools you need for XR development == Unity 2021
2) Setting up your Oculus Quest XR project in Unity 2021!

3) Setting up Mixed-Reality Passthrough for your Oculus Quest XR project in Unity 2021!
4) Keep your XR project safe with a GitHub Code Repo!
5) Setting up Touch Controller for your Oculus Quest XR project in Unity 2021!
6) Coding your Touch Controller for your Oculus Quest XR project in Unity 2021!

Full Step-by-Step Instructions!

  • (Hierarchy) Right Click -> 3D Object -> Sphere

  • (Hierarchy) Click on Sphere -> (Inspector) Change Position X to 1, Z to 0.5 -> Change Scale to X, Y and Z to 0.1
  • (Project) In Scripts -> Double Click on Controllers
  • (Visual Studio) Add the following code to the Controllers class (remember that we created the Controllers class previously)
public Transform trackingSpace;
public GameObject objectToCreate;

public void CreateNewObject(OVRInput.Controller controller)
{
    //--- Controller Tracking
    //--- x, y, z position of the controller in 3D space
    Vector3 position = trackingSpace.TransformPoint(
        OVRInput.GetLocalControllerPosition(controller));
    //--- euler angle (ie. 90 degrees) of the controller
    Vector3 rotation = trackingSpace.TransformDirection(
        OVRInput.GetLocalControllerRotation(controller).eulerAngles);

    //--- Create a new game object at the controller position and angle
    GameObject newObject = Instantiate(
        objectToCreate, position, Quaternion.Euler(rotation));
}
  • (Visual Studio) Still in the Controller class -> scroll down to our Right Trigger code and add the following code
//--- When trigger is pressed create a new object
CreateNewObject(OVRInput.Controller.RTouch);
  • (Hierarchy) Click Player -> drag TrackingSpace into the Tracking Space field in the Controllers (Script) -> drag the Sphere to the Object to Create field in the Controllers (Script)
  • Run your application, you will see that every time you pull your Right Trigger that a sphere appears! But it stays there… How do we make it shoot? We’ll need to tell the sphere what to do when it is created!
  • (Project) Click on Scripts -> Right Click and Create -> C# Script
  • (Project) Double Click on the Shoot script
  • Add the following code in your new Shoot script!
public class Shoot : MonoBehaviour
{
    public float speed = 2.0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //--- On every frame, make the object move the "speed"
        //---  in the forward direction
        //--- Multiply by Time.deltaTime just in case our frame rate varies
        transform.position += Time.deltaTime * speed * transform.forward;
    }
}
  • (Hierarchy) Click on Sphere -> (Project) Drag your Shoot script into the Inspector for your Sphere game object
  • Run your XR application, now when you create your spheres they know what to do! Watch them go… 🙂

One thought on “XR Step-by-Step! XR Game: Create and Shoot in your Meta Quest XR project in Unity 2022!”

Leave a Reply