Beginners guide to UNITY AR Foundation (ARKit & ARCore) - Build your first AR app from scratch!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome to my channel this is my first videos i may sound a nervous a little bit uh so today we'll be building an ar app using the recent unity 2020 and ar foundation as you can see we'll make a placement indicator this red one which when tapped on a game object in this case a spider is spawned in the real world and the indicator goes away i've added a little animation just to make it a little interesting it's doing a push-up for now so to begin if you're really new download unity by typing unity hub download on google here you can either download a specific unity version and the hub or just the unity hub it downloads like a normal software on mac or windows i'm sure most of you have done this thing so uh here what we're choosing is the version 2020.1 this will open up where you see few templates the 3d 2d the high definition one or the universe universal render pipeline for this project we will choose the 3d and name this project ar tutorial and hit create the project has opened up here you've got the hierarchy where all the game objects will be listed the project tab at the bottom will have all your assets at the moment just the scenes folder in this folder you can see a sample scene folder which is opened up in hierarchy on the left first of all this is the basic default layout and we will stick with it to begin creating our project we need to import certain packages from the package manager as you can see here click on this drop down and select the first unity registry uh now start importing patch packages such as ar foundation version 3.1.6 the ar core 3.1.8 then ar kit 3.1.8 after installing the packages you see this xr folder which is visible note please stick to the versions we installed because this unity version and ar foundation ar core and air kit versions else in many cases it won't work moving on to setting up the scene uh we don't need this camera and we'll use an ar camera with xr files we just installed so delete this uh first thing we need is the ar session origin by right clicking in hierarchy uh xr then ar session origin it's a fundamental component uh they are like it's a really fundamental component in the ar setup it's got a session origin script already on it with an ar camera as you see on the right side of the screen in the inspector this ar camera has as you see is more or less like a normal camera in unity but it's got ar post driver ar camera manager and ar camera background scripts sitting on it get the air session by right clicking in hierarchy then exa then ar session which basically run during the ar experience during the whole gameplay it's got a couple of scripts on it which is in which ar session and ar input manager are present these are fine as it is now now we need an indicator so that we can see where do we spawn our game objects let's create an empty game object by right clicking here in the hierarchy and create empty rename this in the inspector on the right as placement indicator now we need to and we need something to show obviously not empty so click on the placement indicator on the left and right clicking in the hierarchy you can select 3d object and a code it's just one face and is a good tool if you do not want to show depth i'll rotate the x in the transform tab on my right in the right inspector panel by 90 and as you see it's now lying flat as i have tested the scale is big we need to reduce it according to my test uh 0.3 is fine for this case so in transfer tab just change the scale to 0.3 in the x y and z now we need to add a script spawn this chord on the flow so let's add an empty game object and call it as a controller on the right in the inspector panel click on add component and start typing the name of the script you wish to create in this case ar placement and hit create double click on the script and it opens visual studio ideally now let's remove the comments here from here and start and update the method we will need in later let's check first if all the scripts we need are already present okay so an ar session we need an ar raycast manager script so add that it already comes in the package we downloaded in the beginning then use add component at the ar plane manager that's it now let's go back to the script let's start by adding public variables the first one would be a game object ar object to spawn then a game object for the placement indicator so we need so we need these two to be inserted from the project and there therefore it's public then let's add a pose called placement pose and a raycast manager variable to access the script we need we we added earlier here it suggests to add this unity unity nginx.xr.ar foundation on the top let's do that else we have to write this whole line whenever we use ar this raycast manager then let's add a boolean placement pose is valid as assign it and assign it as false to begin with now in the start method find the array cast manager component and store it in this arraycast manager variable now in the update what we need is two is a way to update the placement indicator when moving the phone keep a track of the placement position while moving the phone and spawn the ar object now let's make a function for each of these starting with the update placement pose the update placement indicator and let's include them inside the update function now it looks fine but not entirely let's create these methods so avoid update placement indicator and inside we'll have to justify a few conditions to make it update the placement indicator so first let's add a game object where we can store our spawned object now we check if the spawn object equals null and placement pause is valid which is set to false above we need the placement indicators set active true we should see the indicator as there is no object it's null and keep updating its transform that's its position and rotation like this else we make the placement indicator to set active as false which means the air object is spawned now the next method we have is update method we have is the placement pose in this method we need to give the position of the placement indicator in the screen center and using the current camera which is the ar camera at position 0.5 x and 0.5 y which basically is the center of the screen then let's get all the hits and create a list with our hit component on it then using the arraycast manager.traycast and screencenter and the hits we calculated on the trackable planes i'll just add it on the top to make it clean add a little condition that if the dot count is greater than zero then change the boolean accordingly and further check if the placement pose is valid then get the very first hit and store it in the placement pose now finally we add a method to place the ar object simply store it in the spawn object by using instantiate and the ar object which has to instantiate and placement position which is calculated above and rotated and rotation calculated above call this method in the update function when there is nothing inside spawned object and place placement pose is valid and as soon as you touch the screen spawn the ar object if not then through update it will keep updating the placement indicator now go back to unity and put the script file in the scripts folder by right clicking in projects window at the bottom and creating and naming it scripts drag the file by single click on the script file and placing it in the scripts folder now if you click on the controller game object you see you can see two empty fields under ar placement script tab track the placement indicator in the placement indicator field and for the ar object we will get something from the asset asset store if not visible you can check from the top bar click window and asset bar asset store so as you see the asset store is moved click on search online which opens the default browser simply type spider and when you see this free animated spider by prism bucket hit open in the unity in my case whatever it instructs you might have to log into your unity account to access this back in unity you'll see the spider package here as animated spider this should be a download and import button at the bottom simply download first and then import you see a pb spider is available now if i go to these folders here is the model back to the scene view you can see the spider on the right bottom side of the screen enlarge like this to see if you want on looking inside the spider model in the project window it's got its mesh but also a few animation along with it so there's this idle animation jumping animation looks nice now placement indicator looks very boring let's add a target type of an art on it let's head on to the web browser and open a website called flat icon i like it a lot really good for prototyping so i login to my account which by the way is free now search target and let's choose this one change the color and now let's export don't forget attribution is required now i'll drag that file i just downloaded and create a new folder in asset is called textures and materials and drag that file inside texture change this in the inspector to sprite 2d and ui now in the material folder add a new material right clicking in the material folder with create new material and rename as indicator change the shade of the material from standard to unlit and transparent and add the texture we downloaded add this material to a mesh renderer of the quad under the material section okay so now looks like a proper target let's look at it looks good yeah the controller has the placement indicator and now let's put the airgame object let's go to the spider model and let's take out its jump animation out by pressing ctrl d on windows or command d on mac so the jump is out now let's bring the spider into the scene okay it's huge let's scale it down to 0.5 0.5 and 0.5 you know that you know what just change it to 0.2 as even 0.5 was big when tested now to add an animation quickly we need something in this empty animation controller and go animator and animation window at the bottom now selecting the spider model i clicked create in the animation which asked me to add create a folder for this new animation i'll save it in the pb spider folder and call it animation this thing let's say spider now you see this has a spider in its controller so going back to the scene view and actually going to the animator window it should update if you can't see these these windows you can add them from the top window button like here now there is this animation by default attached and we need the jump animation so let's go and search for that let's drag it in the window and then delete the default animation it snaps to a jump animation perfect now just select the jump animation select the loop toggle now the spider is ready simply create a prefab we will use this for the script we created in the controller drag and drop the spider object in the prefab now everything looks ready and now delete the spider from here now some export settings name change it from gamma to linear that's how i prefer okay so i'm not in android mode so through build settings let's switch platform from here by selecting android this might take a minute too you can do it for ios and build it with xcode how it's done normally for any other app okay now add open scene in player settings remove welcome remove multi threading change the minimum api level to 7.0 and everything looks fine in the game view here let's change the resolution to portrait and in player settings in the resolution and presentation select default orientation as portrait just checking the ar camera as i saw a few options were not visible now let's build and here it is this is how it looks obviously you can make it small but this is how you can build an ar app from scratch doesn't it look amazing if you like my work give it a thumbs up subscribe as i will be posting unity related videos possibly every week and don't forget to subscribe and hit the bell icon thank you and peace
Info
Channel: Mohd Hamza
Views: 74,140
Rating: undefined out of 5
Keywords: Augmented reality, Unity 3d
Id: KqzlGApWPEA
Channel Id: undefined
Length: 15min 4sec (904 seconds)
Published: Thu Dec 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.