Adding a Mixed Reality Scoreboard to Meta Quest 3 App in Unity
So for my latest project, I have been working on a mixed reality app for the Meta Quest 3. I have been using Unity to develop the app and I have been using the Unity Depth API to handle the depth for the objects in the scene.
I wanted to add a rudimentary scoreboard to the app that would be visible and keep track and so I decided on playing around with adding a canvas with a score tracking script to update the score every time I hit the balloon in my game.
Here is how it turned out:
Really cool how the Unity Depth API is able to handle the depth of the canvas and have it hide the balloon (even though the baloon goes through the scoreboard).
Here is how I did it:
- I created a new canvas in the scene and gave it the Render Mode of “World Space”.
- I then resized the canvas to be a bit smaller and positioned it where I would like it to be. Notice the scale updates for it:
- I then added a Text using TextMeshPro to the canvas and positioned it where I would like it to be and added some default text to it.
- I then created a score manager object and added a script to it to update the score. I also needed to update my sphere interaction script to call the score manager to update the score.
Here is the script that I used for the score manager:
Score Manager Script
using TMPro; // Make sure to include this
using UnityEngine;
public class ScoreManager : MonoBehaviour
{
public int score = 0; // The player's score
public TextMeshProUGUI scoreText; // Reference to your TextMeshPro component
public void AddScore(int pointsToAdd)
{
score += pointsToAdd;
scoreText.text = "Score: " + score.ToString();
}
}
Here is the updated script for my sphere interaction:
Sphere Interaction Script
using UnityEngine;
using UnityEngine.SceneManagement;
public class SphereInteraction : MonoBehaviour
{
public float forceMultiplier = 5f; // Adjust this to change how hard the sphere is hit
public ScoreManager scoreManager; // Reference to the ScoreManager script
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "GameController")
{
Rigidbody rb = GetComponent();
Vector3 forceDirection = collision.contacts[0].point - transform.position;
forceDirection = -forceDirection.normalized;
Debug.Log("Applying force: " + forceDirection * forceMultiplier);
rb.AddForce(forceDirection * forceMultiplier, ForceMode.Impulse);
// Update the score
if (scoreManager != null)
{
scoreManager.AddScore(1); // Assuming AddScore(1) increases the score by 1
}
// Log the new position
Debug.Log("New Position: " + transform.position);
}
}
}
</code></pre>