Hello everyone, my name is Mike!
A year ago, I made a video about how to create an augmented reality app and it became my
first video that gained 1000 views.
I remember how much I was happy then, a lot has changed since that moment and lately my
videos can't even repeat last year's success, that's why I decided to record another video
about augmented reality, and because I was very surprised that there are practically
no videos on YouTube about creating AR games without using third-party plugins.
Today I will show you how to create an AR-Shooter in few minutes, but I will tell you right
away, this game will be as simple as possible, since I set myself the goal of showing the
development of an AR game as a whole, and not shooting a video for 30 minutes, where
I will tell about uninteresting information about the history of the creation of AR and
the rest of the garbage.
Well, I suggest we move on to creating a game.
Go to Unity Hub, create a new project, select AR.
After creating the project, we change the platform to Android or iOS.
Next, we move all the elements prepared for creating the game to our project: pictures,
fonts, etc.
I also want to say that the elements that I use, you can view by clicking on the link
in the description.
Now I propose to make a user interface, for this we create a canvas.
In UI Scale Mode, we set the second item, Match should be equal to 0.5, and we set reference
resolution according to the screen size that you have selected.
We place a Text on the scene that will display the number of enemies killed.
There will be a scope image in the center, and a firing button on the bottom right.
Now let's add to our AR-camera Rigidbody component with gravity off and Capsule Collider, later
thanks to these elements we will implement the removal of the enemy when colliding with
the player.
Here we need to create a lot of empty objects, the first one is the main, we call it SpawnPoints,
we also call the next ones, only without s at the end, but with numbers, and also choose
a colored icon for them.
We place 8 such points so that they form a circle around our camera.
Well, now the most important thing, enemies, in my game you have to fight with photos of
me and my friend, you can also add those enemies you want, both 2d and 3d.
Let's add a box collider to the enemy, activate the isTrigger property, and write a small
script for it.
In it, we need to define 1 variable that will be responsible for its speed.
In the update method, we use the LookAt command to ensure that the enemy always looks towards
the player.
How do we find out the player's location?, just call the Find method and find his position
by name, then use the Translate function to move.
We transfer the script to the enemy, and make a prefab out of it, and for now we remove
it from the scene, we will make a spawner of our enemies.
For it, we just need to create an empty object and call it a Spawner, then add a script to
this object, we declare 3 lists in it, one will contain spawn points, the second enemies,
and the third UI elements necessary for the game, which will be activated at the beginning
of the battle, as well as 1 button to start the game.
In the EnableSpawner method, we disable this button, which we will create a little later
and display the sight, record and firing button, and run the spawn method, which will be repeated
every 3 seconds.
In the inspector window, we move our spawn points, enemy prefabs and UI to the lists
we announced.
Now we can move on to shooting at our enemies and colliding with them.
To do this, we create a new script for the Ar camera, and declare 2 variables in it,
score and a text displaying this score.
The Shooting method implements a function that removes the enemy and adds 1 point to
the score counter if the sight was pointed at the enemy during the shooting.
The OnTriggerEnter method implements the function of removing an enemy when the player touches
it.
In the Inspector window, we bind all the necessary elements to the script and activate the click
event at the shooting button, for this we transfer
our camera to the object field and select the Shoot method in the Player script.
It remains to make only the main menu.
To do this, we disable all previously created elements and add 1 button, and in the onClick
event we add the Spawner we created, from which we select the EnableSpawner method.
That's all.
After the development is completed, we open the Player Settings window, where we change
the same fields as I did.
These changes will be enough to launch the game on your phone, but not put it on Google
play or the app store, so if you are suddenly going to develop a really cool AR application
for the game store, wait until I release the corresponding video.
And given my breaks in the publication of videos, you will have to wait a long time.
Well, all the settings are set, we press the build button and get the loader file, which
we transfer to android in a convenient way for ourselves, and inconvenient if you have
an iPhone like me.
Open the app and enjoy the fact that now you are not alone in the room.
In my opinion, it turned out well, if you do not take into account that I did not make
the video for 3 months, and I did the game for half an hour.
All for now!