As we are continuing from the previous blog, we have already set up the scene for Plane Detection. Now, all we have to do is write a script that will place an object on the detected plane based on the tapped location.
In this section, we’ll create a new script and write the code.
In this section, we’ll understand how the code works. Feel free to skip to the next section if you understood the program already.
The function Awake() is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is inactive during start up, Awake is not called until it is made active.) So, on Awake, the variables _arRaycastManager is initialized to get the ARRaycastManager components.
Update and Calculation
The function Update() is called every frame when the GameObject is enabled. So, when his function is called:
So now that you know how the code works, let’s move on to the next section.
Now we’ll have to set up our scene to make this work.
With that, we have set up our scene and now it’s time to test it out! So, build the application on your android device by going to File → Build Settings → Build and Run. Make sure that you have connected your device to the machine before building it.
Once it’s built, it’ll lunch on your device. Then, you can scan the floor and tap to place the object and move it around.
Now you know how to use Raycasting to place objects on the detected surface. Apart from Plane Detection and Image Tracking, AR Foundation has other features too, like Face Detection. That's what the next blog post will be about. Stay tuned!
If you've enjoyed the insights shared here, why not spread the word? Share the post with your friends and colleagues who might also find it valuable.
Your support means the world to us and helps us create more content you'll love.