Unity ARKit By Example: Part 3

We create an application that uses hit detection.

Image for post
Image for post

This article is part of a series starting with Unity ARKit By Example: Part 1.

Place Cube

In this example we will use another API from Unity-ARKit-Plugin; we will use its ability to detect hits against objects in the physical world from rays projected out from the center of the camera viewfinder.

In particular, when one presses the Place button, the application will place a 30 cm cube if and where a hit is detected.

Image for post
Image for post

We start by saving the Hello World scene as Place Cube in the Scenes folder and then:

  1. We remove the Cube, Canvas, and EventSystem GameObjects
Image for post
Image for post

We next need to create the model (or prefab) to instantiate the GameObjects from.

  1. We create a Cube GameObject in the scene

We create a script; Assets / Scripts / Place Cube / PlaceCube.cs:

Observations:

  • The code is fairly self-explanatory

We add this script as a component on PlaceCube.

We drag the Cube prefab to the Hit Prefab value on the script.

We then attach the HandleClick method to the PlaceCube button:

  1. On the PlaceCube’s Button component add (plus button) a On Click () entry

We now follow the instructions in the section The Build Process of the article Unity ARKit By Example: Part 1 to build and load the application to an iOS device. A couple of things to keep in mind:

  • We can leave the bundle identifier as we had before

Now running the application on an iOS device, we can see the application working.

note: We have to build and load the application to an iOS device to test the application as ARKitRemote does not support hit detection.

Next Steps

In the next article, Unity ARKit By Example: Part 4, we will continue to explore Unity ARKit.

Written by

Broad infrastructure, development, and soft-skill background

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store