Simple functions in Unity ARcore for Android

Luke Duckett
5 min readAug 1, 2022

--

In this article I will go over how to do some of the basics in Unity AR, using ARCore for Android.

I will cover raycasts using the ARRaycastManager and use this to show how to show a target reticle in the centre of the user's screen if a plane is detected, instantiate an object onto a plane at the position a user taps the screen, and the centre position of the screen and at a random position on the current plane. I will then show how to interact with this object as well.

If you need help setting up the AR environment, you can follow these instructions or read this article about the common components and terminology you may encounter.

For reference, I am using some of the information and assets from the code lab on the Google developer site.

ARFunctionsManager

I’ve created a manager class to handle our AR functions the “ARFunctionsManager”.

The first function I have created casts a ray from the centre of the screen and returns all the ray hits on the plane.

The second function uses the raycast hits from the previous function and uses them to get the current plane.

The third function uses the raycast hits to get the Pose from the centre screen on the current plane.

Targeting Reticle

Our first task is going to be to create a reticle. The model I am using is from the starter package from the Google Starter Package

The reticle is going to have a child game object for the visuals which can be deactivated if there is no current plane detected. We can then set the position of the reticle to the centre of the screen.

On our Reticle script, we call our AR Functions Manager and activate the visuals if a plane is found, then set the position object to the centre of the screen by using our get pose function.

The next step is to add a new game object to house the functions manager and add the reticle to the scene then assign our components to the inspector.

We will have to assign the “ARRaycastManager” and “ARPlaneManager” to the matching variables in the “ARFunctionsManager” script and assign this script to the appropriate variable on our “Reticle” script.

When this is built we can see it in action.

Object Instantiation

I have prefabed a cube which I will spawn using the spawning function created below.

Spawn at center screen point on the current plane

This function is basically the same as the reticle function, we just instantiate our prefab at the position when a touch is detected.

Which results in the following:

Spawn at touch point on the current plane

This task is going to require us to get add a new function to the ARFunctionsManager that takes in a touch and returns a list of ARRaycastHits.

Then on our cube spawnner we can comment out the function that spawns the cube in the center of the screen and replace it with one that spawns at the touch point.

Which results in the following:

Spawn at a random point on the current plane

This method is entirely from the ARCore code labs, it takes the current plane and finds a random triange within the mesh.

Which results in the following:

Interacting with Spawned Objects

Interacting with Spawned Objects is actually performed the same in the AR space as a non AR space, I will still provide an example below.

I have changed the CubeSpawner script to place 6 random cubes on the screen to help with the example.

The next script I have created will be placed directly on the cubes themselves. It will allow the user to tap them 2 times to change the colour and then the final will destroy the cube.

Then I will adjust the prefab by setting the collider to a trigger,changing the material colour to green and adding our SpawnedCube script to it.

The next step is to create a script to handle input to cast rays into the screen to call the interact functions on the cube if the rays hit it. I have created a short script called InputManager to do this.

Make sure to create an object in scene and set up the input manager script on that object.

All together gives us the following result:

That’s all for now.

--

--

Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology