Thereisactually a newversion, sothe 6.0, whichhaschangedsomeofthestufffromtheARFoundationthat I wanttousebecause I wanttomakethistutoriallastaslongaspossible.
So, tomakesurethatfuturepeoplewillbeabletolookatthistutorialrightnowanddothesamestuffasme, well, whatwearegoingtodoisupdatethisARFoundationand, asmentioned, thefirststepistousetheUnityversion 2023 andthesecondversionistodownloadtheUnityARFoundation 6.0.
Now, todownloadthisversionoftheARFoundation, ifwegotoARPackageManager, ARFoundation, andthatwehave a lookattheversionhistory, asyoucansee, wecannotseeanyotherversionrightthereanditcannotbedownloadedherefromthePackageManager, butwhatwecandoisactuallydownloadthenewversionoftheARFoundationbychangingthePackageManifest.
So, letmeclosethis, thenright-clickonAssethereandclickonShowinExplorer.
Now, itshouldopenhereanExplorerpageandifwegoheretoPackages, wecandouble-clickonManifestrightthereandhereyoucanseeallofthepackagesthatareinsidethisprojectandifwegoheretotheXRARFoundation, wecanseetheversionthatweareusingis 5.1.1 and, inmycase, I wanttousethelatestonewhichis 6.0.0.3.5.
So, whatwecandonowiscreatethefourthanchorandforthis, I think I'm goingtoright-click, goto 3D Objectandcreate a verysimple 3D cubethat I'm goingtoputinfrontoftheplayer.
So, let's firstsetitto 0, 0, moveitforward a bitupward, scaleit, maybedo a littlerotation.
So, forthis, weneedtowriteatthetopusingUnityEngine.XR.ARFoundationandthen, wecansimplyremoveheretheUpdatefunctionbecause, asyouwillsee, it's verysimpletoturnthisGameObjectintoananchoratthestartbecauseweonlyneedtodoGameObject.AddComponentandadd a componentoftypeARAnchor.
Andbeautiful!
Now, that's basicallyit.
SimplybyaddingthisARAnchorcomponent, wewillanchorthis 3D cubeinsideourrealworld.
Now, withthis, asyoucansee, thiswilladd a rayinteractorthatwillfollowourleftorrighthand.
Andonethingthat I wanttodoisifweselectboththerightandleftrayinteractorbypressingonthecontrolkey, I wantheretosetthelinebendratiotoonebecause I don't wantthelinetobendatall.
Beautiful.
Now, ifyourememberinthepreviousepisode, whatwasactuallyhappeningwasthattherewas a neardefaultplanewhichwasforeachoneofthewallortheceilingofthefloorthatwehaveinourrealworld.
AndifweclickonthisARdefaultplane, asyoucansee, itisbasically a meshcollider.
So, thisallowedus, ifyouremember, tocreatesomecollisionwithinthescenefromourrealworldtoourvirtualonewhichisawesome.
Butanyway, I wanttousethisARplaneaswellbecause I wanttomakeitfromtherayinteractor.
So, let's clickonaddcomponentandadd a xrsimpleinteractable.
Beautiful.
Andthat's basicallyit.
Withit, weshouldbeabletointeractwiththisplane.
So, what's leftforustodoisnowgobacktotherightandleftrayinteractor.
I'm goingtoselectthembothand I'm goingheretocreate a newcomponentwhichwillbecalledphoneanchorfromrayselect.
Beautiful.
Okay.
So, again, asthenamesuggests, thegoalofthisscriptistospawnananchorfromtherayselection.
So, forthis, wearegoingtoneed a publicreferencetothexrrayinteractorwhichwecancallifwegoatthetopanddousingunityengine.xr.interaction.toolkit.
Andthenthetwovariablesthat I'm goingtoneedis a xrrayinteractorwhich I cancallrayinteractorand a publicARanchormanagercalledanchormanager.
Beautiful.
Now, what I'm goingtodoiscreate a publicvoidfunctionwhichwillbecalledspawnanchorandwhichwilltakeas a parameter a baseinteractioneventargswhich I cancallargs.
So, thegoalofusingthisvariablehereisthatnowinthestartfunction, wecandorayinteractor.selectentered.addlistenerandusehereourspawnanchorfunction.
So, basically, whatwe'vedoneistohookthisspawnanchorfunctiontobecalledwhenevertherayinteractorselectssomething.
So, basically, whatwecandoisthengetthehitpointandthencreateananchorbasedonthishitpointfromtheray.
So, firstthingsfirst, let's getthe 3D raycasthitwithrayinteractor.trygetcurrent 3D raycasthit.
Andwhatwewantiswriteoutraycasthithit.
So, heretheoutraycasthithitwillbasicallybeouroutput.
So, thiswillbetheraycasthitthatiscurrentlyperformedbyourrayinteractor.
Andfromthishitposition, wecanactuallycreate a newposebydoingposehitposeequalsnewposeandwriteherehitdotpointfortheposition.
Andbasically, fortherotation, I wanttotakeintoconsiderationthenormalofthephasethatwehitwithourray.
So, let's doquaterniondotlookrotationminushitdotnormal.
So, hitdotnormalis, as I toldyou, thenormalofthesurfacethatwehit.
Andifwecallthequaterniondotlookrotation, itwillbasicallycreate a quaternion, so a rotation, basedonthisforwardaxis.
So, itwillbasicallyaligneverythingtopointtothisdirection.
Oh, andhere I justwriteminusbecause I wanttherotationtofacetothewallandnotoutofthewall, butactuallyasyouprefer.
Thisisactuallysomethingthat I doherebecause I'm goingtoneeditin a minute.
Butfornow, I'lljustcopywhat I do.
So, nowthatwe'vecreated a newpose, whatwecandoiscreateananchor.
So, wecandoforthisvarresultequalsanchormanagerdottryaddanchorasyncandgiveusaninputorhitpose.
Andhereistheimportantfunction, sothetryaddanchorasync, whichisbasicallyaddingananchorat a certainposition.
Butasyoucansee, thistryandaddanchoris a nothingfunction.