<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Viraj Lakshitha on Medium]]></title>
        <description><![CDATA[Stories by Viraj Lakshitha on Medium]]></description>
        <link>https://medium.com/@virajlakshitha?source=rss-122f555f9b7b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D1Wo5RJEohApTYK7NAW-UA.jpeg</url>
            <title>Stories by Viraj Lakshitha on Medium</title>
            <link>https://medium.com/@virajlakshitha?source=rss-122f555f9b7b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 19:00:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@virajlakshitha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create Simple Multiplayer AR Game with Unity + Vuforia + PUN 2 (for Android)]]></title>
            <link>https://medium.com/teamarimac/create-simple-multiplayer-ar-game-with-unity-vuforia-pun-2-for-android-83ac6b42fcda?source=rss-122f555f9b7b------2</link>
            <guid isPermaLink="false">https://medium.com/p/83ac6b42fcda</guid>
            <category><![CDATA[simple-multiplayer-game]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[vuforia]]></category>
            <category><![CDATA[pun-2]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Viraj Lakshitha]]></dc:creator>
            <pubDate>Mon, 07 Dec 2020 13:50:37 GMT</pubDate>
            <atom:updated>2020-12-07T15:48:10.771Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*33HAg95NdnEvFTPGOejNDw.png" /></figure><h3>Introduction</h3><p><strong>Augmented Reality (AR)</strong> is one of the most actively developing technology side today. In the Gaming industry, we can create many interactive games with AR technology. In this tutorial, I am going to explain an easy way to create an <strong>Android Multiplayer AR Game</strong> using the <strong><em>Unity Engine</em></strong>.</p><h3>Prerequisites</h3><ul><li><strong>Unity Engine</strong>: You can download it <a href="https://unity3d.com/get-unity/download"><strong><em>Here</em></strong></a><em>.</em></li><li><strong>Vuforia Plugin</strong>: Vuforia is a software platform to develop <strong>Augmented Reality (AR)</strong> applications. Vuforia support in <strong>Android</strong>, <strong>iOS</strong>, and <strong>UWP</strong> platforms. You can download Vuforia from <a href="https://developer.vuforia.com/downloads/sdk"><strong><em>Here</em></strong></a>.</li><li><strong>PUN 2</strong>: <strong>P</strong>hoton<strong> U</strong>nity <strong>N</strong>etwork (<strong>PUN</strong>) is a real-time <strong>Multiplayer Game development</strong> framework for Unity Engine. You can download PUN 2 from <a href="https://assetstore.unity.com/packages/tools/network/pun-2-free-119922"><strong><em>Here</em></strong></a>.</li></ul><h3>So, Let’s get started…</h3><p>Hello Guys, Let’s start this tutorial from now on. First of all, I’m going to split this tutorial into 2 parts.</p><ol><li><strong><em>Create a multiplayer room</em></strong></li><li><strong><em>Add simple AR game into Game App</em></strong></li></ol><h3><strong>1. Create The Room with PUN 2 :</strong></h3><p>First, create a Unity project to start our Game. Here I’m using Unity 2018.4 version. But you can use any Unity version for this. After creating the project, Go to : <strong>File &gt; Build Settings</strong> and Switch Platform to <strong>Android</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*nvcCDS1hohsjjJE33OyBOA.png" /><figcaption>Select Android and click the “Switch Platform” Button</figcaption></figure><p>Then you need to download and import <strong>PUN 2</strong> Unity package into your project. After importing the PUN 2 Package, You should enter <strong><em>AppId</em></strong> to setup PUN for your project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*_M9ZvrN8_bO8G7ZgsmPPtg.png" /></figure><p>Next, create 2 scenes in your Project. <strong>File &gt; New Scene</strong> and save scenes with different names (like “Main”, “Game”).</p><p>Double click and open Main Scene. Then create an Empty GameObject in the main scene hierarchy and rename it as “Game Manager”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*Ayp28pxaED7d2ImWEMoeQA.png" /><figcaption><strong>Right Click &gt; Create Empty</strong></figcaption></figure><p>Next, select the “Game Manager” Game object and go to Inspector of the Game Object. Create and Add “<strong><em>MenuManager</em></strong>” new script as a <strong>Game Manager</strong> object Component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*olEcCF4ySIcJv4fCPKq9JA.png" /><figcaption>Type “<strong>MenuManager</strong>” <strong>&gt;</strong> New script <strong>&gt;</strong> Create and Add</figcaption></figure><p>Create and Add another new script as “<strong><em>NetworkManager</em></strong>” in <strong>Game Manager</strong> Object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*WJFek1UQYp4NN0fsPcujbg.png" /></figure><p>Now, Open “ <strong><em>NetworkManager.cs”</em></strong> in the editor and copy and paste the script below and save the script.</p><pre><strong>using</strong> System.Collections;</pre><pre><strong>using</strong> System.Collections.Generic;</pre><pre><strong>using</strong> UnityEngine;</pre><pre><strong>using</strong> Photon.Pun;</pre><pre><strong><em>public</em> class</strong> NetworkManager : <strong>MonoBehaviourPunCallbacks</strong></pre><pre>{</pre><pre><strong><em>public</em></strong> <strong>static NetworkManager</strong> instance;</pre><pre><strong><em>private</em></strong> <strong>void</strong> Awake()</pre><pre>{</pre><pre><strong>if</strong>(instance != null &amp;&amp; instance != this)</pre><pre>{</pre><pre>gameObject.SetActive(false);</pre><pre>}</pre><pre><strong>else</strong></pre><pre>{</pre><pre>instance = this;</pre><pre>DontDestroyOnLoad(gameObject);</pre><pre>}</pre><pre>}</pre><pre><strong><em>private</em> void</strong> Start()</pre><pre>{</pre><pre>PhotonNetwork.ConnectUsingSettings();</pre><pre>}</pre><pre><strong><em>public </em>void</strong> CreateRoom(string roomName)</pre><pre>{</pre><pre>PhotonNetwork.CreateRoom(roomName);</pre><pre>}</pre><pre><strong><em>public </em>void </strong>JoinRoom(string roomName)</pre><pre>{</pre><pre><strong>if</strong>(PhotonNetwork.PlayerList.Length &lt;= 4)</pre><pre>{</pre><pre>PhotonNetwork.JoinRoom(roomName);</pre><pre>}</pre><pre>}</pre><pre><strong>[PunRPC]</strong></pre><pre><strong><em>public </em>void </strong>ChangeScene(<strong>string</strong> sceneName)</pre><pre>{</pre><pre>PhotonNetwork.LoadLevel(sceneName);</pre><pre>}</pre><pre>}</pre><p>Then, Open “<strong><em>MenuManager.cs</em></strong>” in the editor and copy and paste the scripts below and save the script.</p><pre><strong>using</strong> System.Collections;</pre><pre><strong>using</strong> System.Collections.Generic;</pre><pre><strong>using</strong> UnityEngine;</pre><pre><strong>using</strong> UnityEngine.UI;</pre><pre><strong>using</strong> Photon.Pun;</pre><pre><strong>using</strong> Photon.Realtime;</pre><pre><strong><em>public</em> class</strong> MenuManager : <strong>MonoBehaviourPunCallbacks</strong></pre><pre>{</pre><pre><strong>[Header(“ — -Menus — -”)]</strong></pre><pre><strong><em>public</em></strong> <strong>GameObject</strong> mainMenu;</pre><pre><strong><em>public</em></strong> <strong>GameObject</strong> lobbyMenu;</pre><pre><strong>[Header(“ — -Main Menu — -”)]</strong></pre><pre><strong><em>public</em></strong> <strong>Button</strong> createRoomBtn;</pre><pre><strong><em>public</em></strong> <strong>Button</strong> joinRoomBtn;</pre><pre><strong>[Header(“ — -Lobby Menu — -”)]</strong></pre><pre><strong><em>public</em></strong> <strong>Text</strong> roomName;</pre><pre><strong><em>public</em></strong> <strong>Text</strong> playerList;</pre><pre><strong><em>public</em></strong> <strong>Button</strong> startGameBtn;</pre><pre><strong><em>private</em></strong> <strong>void</strong> Start()</pre><pre>{</pre><pre>createRoomBtn.interactable = false;</pre><pre>joinRoomBtn.interactable = false;</pre><pre>}</pre><pre><strong><em>public</em></strong> <strong>override void</strong> OnConnectedToMaster()</pre><pre>{</pre><pre>createRoomBtn.interactable = true;</pre><pre>joinRoomBtn.interactable = true;</pre><pre>}</pre><pre><strong>void</strong> SetMenu(<strong>GameObject</strong> menu)</pre><pre>{</pre><pre>mainMenu.SetActive(false);</pre><pre>lobbyMenu.SetActive(false);</pre><pre>menu.SetActive(true);</pre><pre>}</pre><pre><strong><em>public</em></strong> <strong>void</strong> OnCreateRoomBtn(<strong>Text</strong> roomNameInput)</pre><pre>{</pre><pre>NetworkManager.instance.CreateRoom(roomNameInput.text);</pre><pre>roomName.text = roomNameInput.text;</pre><pre>}</pre><pre><strong><em>public</em></strong> <strong>void</strong> OnJoinRoomBtn(<strong>Text</strong> roomNameInput)</pre><pre>{</pre><pre>NetworkManager.instance.JoinRoom(roomNameInput.text);</pre><pre>roomName.text = roomNameInput.text;</pre><pre>}</pre><pre><strong><em>public </em>void</strong> OnPlayerNameUpdate(<strong>Text</strong> playerNameInput)</pre><pre>{</pre><pre>PhotonNetwork.NickName = playerNameInput.text;</pre><pre>}</pre><pre><strong><em>public </em>override void</strong> OnJoinedRoom()</pre><pre>{</pre><pre>SetMenu(lobbyMenu);</pre><pre>photonView.RPC(“UpdateLobbyUI”, RpcTarget.All);</pre><pre>}</pre><pre><strong><em>public </em>override void</strong> OnPlayerLeftRoom(<strong>Player </strong>otherPlayer)</pre><pre>{</pre><pre>UpdateLobbyUI();</pre><pre>}</pre><pre><strong>[PunRPC]</strong></pre><pre><strong><em>public </em>void</strong> UpdateLobbyUI()</pre><pre>{</pre><pre>playerList.text = “”;</pre><pre><strong>foreach</strong>(<strong>Player</strong> player <strong>in</strong> PhotonNetwork.PlayerList)</pre><pre>{</pre><pre><strong>if</strong> (player.IsMasterClient)</pre><pre>{</pre><pre>playerList.text += player.NickName + “ (Host) \n”;</pre><pre>}</pre><pre><strong>else</strong></pre><pre>{</pre><pre>playerList.text += player.NickName + “\n”;</pre><pre>}</pre><pre>}</pre><pre><strong>if</strong> (PhotonNetwork.IsMasterClient)</pre><pre>{</pre><pre>startGameBtn.interactable = true;</pre><pre>}</pre><pre><strong>else</strong></pre><pre>{</pre><pre>startGameBtn.interactable = false;</pre><pre>}</pre><pre>}</pre><pre><strong><em>public </em>void</strong> OnLeaveLobbyBtn()</pre><pre>{</pre><pre>PhotonNetwork.LeaveRoom();</pre><pre>SetMenu(mainMenu);</pre><pre>}</pre><pre><strong><em>public </em>void</strong> OnStartGameBtn()</pre><pre>{</pre><pre>NetworkManager.instance.photonView.RPC(“ChangeScene”, RpcTarget.All, “<strong>Game</strong>”);</pre><pre>}</pre><p>In the last method, “Game” means the name of the next scene (“Game” scene).</p><p>Now, we have to make simple UIs for <strong>Main menu</strong> and <strong>Lobby Menu</strong> in Main Scene.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7FuxfBAHYGHWnCb6AV-qNQ.png" /><figcaption>Main Menu</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nQBZ1YDzRd5Fftw-mD0tvw.png" /><figcaption>Lobby Menu</figcaption></figure><p>You can create a Simple Main menu and Lobby like this. Here I used UI texts and UI buttons. You can select UI Objects Like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*ro-bglj2k-Xx1XlBtUYv0Q.png" /><figcaption><strong>Right Click</strong> on <strong>Hierarchy</strong> &gt;<strong> UI </strong>&gt;<strong> Text/Button</strong></figcaption></figure><p>Then we need to fill <strong><em>public</em></strong> variables in <strong>MenuManager </strong>script. First, select the <strong>GameManager</strong> gameobject in Hierarchy. Drag and drop objects from <strong>Hierarchy</strong> to <strong>MenuManager script</strong> variables like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLcDCkTqNcGK6ZYagAz4kw.png" /><figcaption><strong>Drag and drop the numbers in order</strong></figcaption></figure><p><em>Then we need to assign functions to buttons and inputFields in the </em><strong><em>Main Menu</em></strong><em> and </em><strong><em>Lobby</em></strong><em>.</em></p><p><strong>Main Menu :</strong></p><ul><li><strong>Player Name Input field</strong></li></ul><p>Select Player Name input Field gameObject and go to the inspector. Then add an OnValueChanged function as <strong><em>MenuManager.OnPlayerNameUpdate </em></strong>.<strong><em> </em></strong>Then add the <strong>Text</strong> child object of <strong>PlayerName gameObject</strong> as the OnPlayerNameUpdate function <strong>parameter</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uzDSd63Fh4yNBOrG8gtIng.png" /><figcaption><strong>Drag and drop Numbered gameObjects from Hierarchy to Inspector</strong></figcaption></figure><ul><li><strong>CreateRoom Button</strong></li></ul><p>Select CreateRoomBtn button and add <strong><em>MenuManager.OnCreateRoomBtn</em></strong> as <strong>OnClick()</strong> function. Then add <strong>Text</strong> child object of <strong>RoomName gameObject</strong> as the <strong><em>OnCreateRoomBtn</em></strong> function <strong>parameter</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GuqinnJwQPRVv73gB1fR_w.png" /><figcaption><strong>Drag and drop Numbered gameObjects from Hierarchy to Inspector</strong></figcaption></figure><ul><li><strong>JoinRoom Button</strong></li></ul><p>Select JoinRoomBtn button and add <strong><em>MenuManager.OnJoinRoomBtn</em></strong> as OnClick() function. Then add <strong>Text</strong> child object of <strong>RoomName gameObject</strong> as the <strong><em>OnJoinRoomBtn</em></strong> function <strong>parameter</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6rmDsPsYeE_iA5fIQJ-DXQ.png" /><figcaption><strong>Drag and drop Numbered gameObjects from Hierarchy to Inspector</strong></figcaption></figure><p><strong>Lobby Menu :</strong></p><ul><li><strong>LeaveLobby Button</strong></li></ul><p>Select Leavelobby Button and add <strong><em>MenuManager.OnLeaveLobbyBtn</em></strong> as OnClick() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UNnbc77XJqhieXGyYnsK6w.png" /><figcaption><strong>Drag and drop Numbered gameObjects from Hierarchy to Inspector</strong></figcaption></figure><ul><li><strong>StartGame Button</strong></li></ul><p>Select StartGame button and add <strong><em>MenuManager.OnStartGameBtn</em></strong> as OnClick() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gkc1ZVQaz4Dtn_lZ2PzdRQ.png" /><figcaption><strong>Drag and drop Numbered gameObjects from Hierarchy to Inspector</strong></figcaption></figure><p>Then, select GameManager gameObject and add <strong><em>Photon View</em></strong> script to the object’s Inspector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CFPcxIfqMzmPUdb4ie8FrQ.png" /><figcaption>Add <strong>Photon View</strong> componnet</figcaption></figure><p><strong><em>**** Now, the Multiplayer part of the Game is finished. Then, we have to create the AR part.</em> ****</strong></p><h3>Let’s start it…</h3><h3>2. Create Simple AR Game :</h3><p>In this tutorial, I’m using the Vuforia plugin for the AR part. After importing the Vuforia plugin for our project, we need to <strong>ON</strong> vuforia Augmented Reality support in <strong>Player Settings</strong>. For that, Go to : <strong>File &gt; Build Settings &gt; Player Settings &gt; XR Settings</strong> and Check the <strong><em>Vuforia Augmented Reality Supported</em></strong> tick. <strong>(</strong> In 2019 and the latest Unity versions no need to do this Step<strong>).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*bNmnPZwRPq7bcrmNe3jjeg.png" /><figcaption><strong>File &gt; Build Settings &gt; Player Settings</strong></figcaption></figure><p>Then you need to add our <strong>Main</strong> and <strong>Game</strong> scenes into Build Scenes. First Double Click and open the <strong>Main</strong> Scene and Go to <strong>File &gt; Build Settings</strong>. Then Click the <strong><em>Add Open Scenes</em></strong> button under the <strong>Scenes in Built</strong> to add opened Scene to Build. Then open the <strong>Game</strong> scene and add the Scene to Build Scenes like the first one. <strong><em>(</em></strong><em>Remember this </em><strong><em>scene ID</em></strong><em> of </em><strong><em>Main Scene</em></strong><em> Must be 0</em><strong><em>).</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*RE9FHtPzdIZ5PsX6vRdINQ.png" /></figure><p><strong>**** OK, now let’s Start the Simple AR Game ****</strong></p><p>Open the <strong>Game</strong> Scene. Then create a new script named <strong><em>GameManager.cs</em></strong> and copy and paste the scripts below into the GameManager script.</p><pre><strong>using</strong> System.Collections;</pre><pre><strong>using</strong> System.Collections.Generic;</pre><pre><strong>using</strong> UnityEngine;</pre><pre><strong>using</strong> Photon.Pun;</pre><pre><strong>using</strong> Photon.Realtime;</pre><pre><strong>using</strong> System.Linq;</pre><pre><strong><em>public </em>class</strong> GameManager : <strong>MonoBehaviourPunCallbacks</strong></pre><pre>{</pre><pre>[<strong>Header</strong>(“Status”)]</pre><pre><strong><em>public </em>bool </strong>gameEnded = false;</pre><pre>[<strong>Header</strong>(“Players”)]</pre><pre><strong><em>public </em>string</strong> playerPrefabLocation;</pre><pre><strong><em>public</em> Transform[]</strong> spawnPoints;</pre><pre><strong><em>public</em> PlayerController[]</strong> players;</pre><pre><strong><em>private </em>int</strong> playersInGame;</pre><pre><strong><em>private </em>List&lt;int&gt;</strong> pickedSpawnIndex;</pre><pre>[<strong>Header</strong>(“Reference”)]</pre><pre><strong><em>public </em>GameObject</strong> imageTarget;</pre><pre>//instance</pre><pre><strong><em>public </em>static</strong> GameManager instance;</pre><pre><strong><em>private </em>void</strong> Awake()</pre><pre>{</pre><pre>instance = this;</pre><pre>}</pre><pre><strong><em>private </em>void</strong> Start()</pre><pre>{</pre><pre>pickedSpawnIndex = new List&lt;int&gt;();</pre><pre>players = new <strong>PlayerController[</strong>PhotonNetwork.PlayerList.Length<strong>]</strong>;</pre><pre>photonView.RPC(“ImInGame”, RpcTarget.AllBuffered);</pre><pre>DefaultTrackableEventHandler.isTracking = false;</pre><pre>}</pre><pre><strong><em>private </em>void</strong> Update()</pre><pre>{</pre><pre>Debug.Log(“is tracking “ + DefaultTrackableEventHandler.isTracking);</pre><pre><strong>foreach</strong> (<strong>GameObject </strong>gameObj in GameObject.FindObjectsOfType(typeof(<strong>GameObject</strong>)))</pre><pre>{</pre><pre><strong>if </strong>(gameObj.name == “Player(Clone)”)</pre><pre>{</pre><pre>gameObj.transform.SetParent(imageTarget.transform);</pre><pre>}</pre><pre>}</pre><pre><strong>for</strong> (<strong>int </strong>i = 1; i &lt; imageTarget.transform.childCount; i++)</pre><pre>{</pre><pre>imageTarget.transform.GetChild(i).gameObject.SetActive(DefaultTrackableEventHandler.isTracking);</pre><pre>}</pre><pre>}</pre><pre><strong>[PunRPC]</strong></pre><pre><strong>void</strong> ImInGame()</pre><pre>{</pre><pre>playersInGame++;</pre><pre><strong>if</strong>(playersInGame == PhotonNetwork.PlayerList.Length)</pre><pre>{</pre><pre>SpawnPlayer();</pre><pre>}</pre><pre>}</pre><pre><strong>void</strong> SpawnPlayer()</pre><pre>{</pre><pre>int rand = Random.Range(0, spawnPoints.Length);</pre><pre><strong>while </strong>(pickedSpawnIndex.Contains(rand))</pre><pre>{</pre><pre>rand = Random.Range(0, spawnPoints.Length);</pre><pre>}</pre><pre>pickedSpawnIndex.Add(rand);</pre><pre><strong>GameObject</strong> playerObject = PhotonNetwork.Instantiate(playerPrefabLocation, spawnPoints[rand].position, Quaternion.identity);</pre><pre>//intialize the player</pre><pre><strong>PlayerController </strong>playerScript = playerObject.GetComponent&lt;PlayerController&gt;();</pre><pre>playerScript.photonView.RPC(“Initialize”, RpcTarget.All, PhotonNetwork.LocalPlayer);</pre><pre>}</pre><pre><strong><em>public</em> PlayerController</strong> GetPlayer(int playerID)</pre><pre>{</pre><pre><strong>return </strong>players.First(x =&gt; x.id == playerID);</pre><pre>}</pre><pre><strong><em>public </em>PlayerController</strong> GetPlayer(GameObject playerObj)</pre><pre>{</pre><pre><strong>return </strong>players.First(x =&gt; x.gameObject == playerObj);</pre><pre>}</pre><pre>}</pre><p>Then I’m using <strong>Unity Standard Assets</strong> plugin for my game’s player controls. So, first, go to<strong><em> </em></strong><a href="https://assetstore.unity.com/packages/essentials/asset-packs/standard-assets-for-unity-2018-4-32351"><strong><em>this Link</em></strong></a><strong><em> </em></strong>to download the Standard Asset package and then import it into our project.</p><p>Next, create another script named <strong><em>PlayerController.cs</em></strong> and copy and paste the scripts below into PlayerController script.</p><pre><strong>using </strong>System.Collections;</pre><pre><strong>using </strong>System.Collections.Generic;</pre><pre><strong>using </strong>UnityEngine;</pre><pre><strong>using </strong>Photon.Pun;</pre><pre><strong>using </strong>Photon.Realtime;</pre><pre><strong>using </strong>UnityEngine.UI;</pre><pre><strong>using </strong>UnityStandardAssets.CrossPlatformInput;</pre><pre><strong><em>public </em>class</strong> PlayerController : <strong>MonoBehaviourPunCallbacks</strong></pre><pre>{</pre><pre>[<strong>HideInInspector</strong>]</pre><pre><strong><em>public </em>int</strong> id;</pre><pre>[<strong>Header</strong>(“Component”)]</pre><pre><strong><em>public </em>Rigidbody</strong> rig;</pre><pre><strong><em>public </em>Player</strong> photonPlayer;</pre><pre><strong><em>public</em> Text</strong> playerNickName;</pre><pre>[<strong>SerializeField</strong>]</pre><pre><strong><em>private </em>float</strong> speed = 0.2f;</pre><pre><strong>[PunRPC]</strong></pre><pre><strong><em>public</em> void</strong> Initialize(<strong>Player </strong>player)</pre><pre>{</pre><pre>photonPlayer = player;</pre><pre>id = player.ActorNumber;</pre><pre>speed = 0.2f;</pre><pre>GameManager.instance.players[id — 1] = this;</pre><pre><strong>if </strong>(!photonView.IsMine)</pre><pre>{</pre><pre>rig.isKinematic = true;</pre><pre>}</pre><pre>}</pre><pre><strong><em>private </em>void</strong> Start()</pre><pre>{</pre><pre>speed = 0.2f;</pre><pre>rig.isKinematic = true;</pre><pre>playerNickName.text = photonPlayer.NickName;</pre><pre>}</pre><p>Then we need to add Player Movements. In this project, I used manual Player Movements. Add below <strong>Movements()</strong> function and <strong>Update()</strong> function into <strong><em>PlayerController.cs</em></strong> script.</p><pre><strong><em>private </em>void</strong> Update()</pre><pre>{</pre><pre><strong>if</strong> (photonPlayer.IsLocal)</pre><pre>{</pre><pre>Movements();</pre><pre><strong>if</strong> (Input.GetKey(KeyCode.LeftControl) || CrossPlatformInputManager.GetButton(“Shoot”))</pre><pre>{</pre><pre>photonView.RPC(“Fire”, RpcTarget.All);</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre><strong>void </strong>Movements()</pre><pre>{</pre><pre><strong>float </strong>horizontal = CrossPlatformInputManager.GetAxisRaw(“Horizontal”);</pre><pre><strong>float </strong>vertical = CrossPlatformInputManager.GetAxisRaw(“Vertical”);</pre><pre><strong>float </strong>hori = Input.GetAxis(“Horizontal”);</pre><pre><strong>float </strong>verti = Input.GetAxis(“Vertical”);</pre><pre><strong>if</strong>(horizontal != 0 || vertical != 0 || hori !=0 || verti != 0)</pre><pre>{</pre><pre>speed = 0.2f;</pre><pre>}</pre><pre><strong>else</strong></pre><pre>{</pre><pre>speed = 0;</pre><pre>}</pre><pre><strong>if </strong>((horizontal &gt; 0 &amp;&amp; vertical &gt; 0) || (hori &gt; 0 &amp;&amp; verti &gt; 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, 45, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((horizontal &gt; 0 &amp;&amp; vertical &lt; 0) || (hori &gt; 0 &amp;&amp; verti &lt; 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, 135, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((horizontal &lt; 0 &amp;&amp; vertical &lt; 0) || (hori &lt; 0 &amp;&amp; verti &lt; 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, -135, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((horizontal &lt; 0 &amp;&amp; vertical &gt; 0) || (hori &lt; 0 &amp;&amp; verti &gt; 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, -45, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((horizontal &gt; 0 &amp;&amp; vertical == 0) || (hori &gt; 0 &amp;&amp; verti == 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, 90, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if </strong>((horizontal &lt; 0 &amp;&amp; vertical == 0) || (hori &lt; 0 &amp;&amp; verti == 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, -90, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((vertical &gt; 0 &amp;&amp; horizontal == 0) || (verti &gt; 0 &amp;&amp; hori == 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, 0, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre><strong>else if</strong> ((vertical &lt; 0 &amp;&amp; horizontal == 0) || (verti &lt; 0 &amp;&amp; hori == 0))</pre><pre>{</pre><pre>transform.localEulerAngles = new Vector3(0, 180, 0);</pre><pre>transform.Translate(Vector3.forward * speed * Time.deltaTime);</pre><pre>}</pre><pre>}</pre><p>In this script, we can use move buttons in the display or keyboard arrow keys to move the player.</p><p>Then let’s add a <strong>Fire()</strong> function for shoot the players. Copy and Paste the script below into <strong><em>PlayerController.cs</em></strong> script.</p><pre><strong>[PunRPC]</strong></pre><pre><strong>void </strong>Fire()</pre><pre>{</pre><pre><strong>GameObject</strong> bullet = Instantiate(Resources.Load(“bullet”, typeof(<strong>GameObject</strong>))) as <strong>GameObject</strong>;</pre><pre>bullet.name = photonPlayer.NickName;</pre><pre><strong>Rigidbody</strong> rb = bullet.GetComponent&lt;Rigidbody&gt;();</pre><pre>bullet.transform.localPosition = transform.position;</pre><pre>bullet.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);</pre><pre>rb.AddForce(this.transform.forward * 300f);</pre><pre>Destroy(bullet, 1);</pre><pre>}</pre><p>Then, let’s create a simple script when the <strong>player </strong>is hit by a <strong>bullet.</strong> Copy and Paste the scripts below into <strong><em>PlayerController.cs</em></strong> script.</p><pre><strong><em>private</em> void</strong> OnTriggerEnter(<strong>Collider </strong>other)</pre><pre>{</pre><pre><strong>if </strong>(other.tag == “bullet”)</pre><pre>{</pre><pre><strong>if </strong>(other.name != photonPlayer.NickName)</pre><pre>{</pre><pre>Debug.Log(“hited”);</pre><pre><strong>StartCoroutine</strong>(PlayerColorChange());</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre><strong>IEnumerator </strong>PlayerColorChange()</pre><pre>{</pre><pre>this.gameObject.GetComponent&lt;MeshRenderer&gt;().material.color = Color.red;</pre><pre><strong><em>yield </em>return new </strong>WaitForSeconds(2);</pre><pre>this.gameObject.GetComponent&lt;MeshRenderer&gt;().material.color = Color.green;</pre><pre>}</pre><p>Now, create a prefab object as “<strong>Player</strong>”.(<strong><em>You can create a prefab of game Object like, Add the Player game object to Hierarchy in Game scene and drag and drop the Player gameobject again to the Assets folder</em></strong>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*eEeExQ5eY66G7M6v32b3Yg.png" /></figure><p>Then Double click and open the Player prefab in Asset folder. Then Add <strong><em>PlayerController.cs</em></strong> script into PlayerPrefab. Then click Add Component button and <strong>Add PhotonView</strong> and <strong>PhotonTransformView</strong> scripts into <strong>Player Prefab</strong>. Then, Add a <strong>Rigidbody</strong> component into Player prefab and ON <strong>isKinematic</strong> tick and Freeze <strong>X,Y</strong> rotations in Rigidbody.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*tcaduhMq2WcYD0LPEM3MuQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*VmXbTEgVOgBebpKhX0zCRw.png" /></figure><p>Then Move Player prefab into <strong>Resources</strong> folder.Then we need to create a bullet prefab.(you can use <strong>Sphere </strong>gameObject as bullet).Rename it as “bullet” and add a <strong>Tag </strong>to bullet prefab as “bullet”. Then go to <strong>Sphere Collider</strong> component of bullet prefab and check(ON) <strong>isTrigger </strong>value. Then add <strong>Rigidbody </strong>component into bullet prefab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*aRW4h6IFw7baE-U1lF3OHg.png" /><figcaption><strong>Click Tag dropdown and select “Add Tag…” and create a new tag as “bullet”</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*tCNRzD5H-W1cszbOUffxgw.png" /></figure><p><strong><em>***Then you need to delete bullet and Player prefabs in Hierarchy.***</em></strong></p><h3>AR Configuration</h3><p>Now, we need to create <strong>ARCamera </strong>and <strong>ImageTarget</strong>. For that right click in Hierarchy and Go to : <strong>Vuforia Engine &gt; AR Camera</strong></p><p>Now we must configure the vuforia settings with this steps.</p><ol><li>Go to <strong>Vuforia Developer Portal </strong>site and log into it.<a href="https://developer.vuforia.com/vui/develop/licenses"><em>Click Here</em></a><em> </em><strong>(If you don’t have an vuforia account you need to create one)</strong>.</li><li>Then go to<strong> License Manager</strong> in Developer site and create a <strong>Development Key </strong>and copy the License key.</li><li>Go to the project and select ARCamera. Then click <strong>Open Vuforia configuration</strong> in ARCamera inspector.</li><li>Paste the License Key into App License Key.</li></ol><p>Now you need to add a <strong>vuforia target database </strong>with this steps.</p><ol><li>. Go to<strong> Target Manager</strong> in Vuforia Developer site and Add database.</li><li>Add a Database <strong>name</strong> and select the type as <strong>Device</strong> and create.</li><li>Then open the created database and click <strong>Add Target </strong>button and upload a suitable <strong>single target Image </strong>with 1 of width</li><li>Then click <strong>Download Database</strong> button and select the Platform as <strong>Unity Editor</strong>.Then, download the database <strong>UnityPackage.</strong></li><li>Now, open the downloaded unitypackage and import into our project.</li><li>Now, automatically added your vuforia database into <strong>Databases </strong>of VuforiaConfiguration.</li></ol><p>Now Your vuforia configuration is over.</p><p>Then we need to create an ImageTarget. For that right click in Hierarcy and Go to : <strong>Vuforia Engine &gt; Image</strong></p><p>Then it will be automatically add your target image in database as <strong>ImageTarget</strong>.</p><p>Then we need change a small variable in DefaultTarckableEventHanler script in ImageTarget object. Open the script and create a variable as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*7rUOKk9bGfu1GCXhi2lLMA.png" /><figcaption><strong>DefaultTrackableEventHandler </strong>script</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*pdEx9Xe8R5If6C1Rb7Tzyg.png" /><figcaption><strong>DefaultTrackableEventHandler </strong>script</figcaption></figure><p>Now we need to add a <strong>plane </strong>as ground in our Game. Right click on <strong>ImageTarget</strong> and Go to : <strong>3D Object &gt; Plane </strong>and create a plane in ImageTarget Object.</p><p>Next, create an Empty gameObject as <strong>SpawnPoints</strong>. Then create 4 Empty objects as points and change their position on ground Plane with different from each other.(change only X and Z values).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A5nvqP6jQmJsY22n3Ee1FQ.png" /><figcaption><strong>Each points must have a different X and Z positions(don’t change Y position)</strong></figcaption></figure><p>Now, create Empty gameObject as <strong>GameManager </strong>in Hierarchy. Then add <strong><em>GameManager.cs</em></strong> script into GameManager object. then go to Add Component and add <strong><em>PhotonView </em></strong>script and change <strong>ViewID </strong>of that as 2 .</p><p>Then Drag and Drop the reference variables in to <strong><em>GameManager</em></strong> script like below. (Type “Player” as <strong>Player Prefab Location</strong>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ah8ZR6ra42KdYTQTUhgD_w.png" /><figcaption><strong>Drag and Drop the GameObject in numbers order</strong></figcaption></figure><p>Now we need to create a Control buttons panel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*2m2JtTQt0LI1x_WLG1LW8A.png" /></figure><p>Here I created simple controller UI with <strong>UI buttons</strong> for <strong>Movements </strong>and <strong>Shoot</strong>.</p><p>Then we need to add function for those Buttons. First, add <strong><em>AxisTouchButton </em></strong>script into <strong>Movement </strong>buttons(Left, Right, Up, Down). <strong><em>To use this script, you must import Unity Standard Assets packages first.</em></strong></p><p>Then set <strong><em>AxisTouchButton </em></strong>script values like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YIc0rl2akionaF0c3g-nUg.png" /><figcaption><strong>Left</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bG0h-JnVTWhfaKyeEcFRjA.png" /><figcaption><strong>Right</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EDByZvaumR7KuCuDLMNb-w.png" /><figcaption><strong>Up</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83u3PaOOdvpDx-4faq9NqA.png" /><figcaption><strong>Down</strong></figcaption></figure><p>Next, Add <strong><em>ButtonHandler </em></strong>script and <strong><em>EventTrigger </em></strong>to <strong>Shoot button</strong> as below and Type “Shoot” as <strong>Name</strong> variable in <strong><em>ButtonHandler </em></strong>script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-TXjw4xkMmH4aX03NQK7Q.png" /></figure><p>This is the end of this tutorial and you can add more things like Animations, Levels, Control function and more.</p><h3>***********************************************</h3><p><strong><em>***Now you can build this Game and Play this Simple Multiplayer AR Game.***</em></strong></p><h3>Thank you… Enjoy !!!</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83ac6b42fcda" width="1" height="1" alt=""><hr><p><a href="https://medium.com/teamarimac/create-simple-multiplayer-ar-game-with-unity-vuforia-pun-2-for-android-83ac6b42fcda">Create Simple Multiplayer AR Game with Unity + Vuforia + PUN 2 (for Android)</a> was originally published in <a href="https://medium.com/teamarimac">Arimac</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>