{"id":259,"date":"2016-02-28T21:11:47","date_gmt":"2016-02-28T21:11:47","guid":{"rendered":"http:\/\/www.doofah.com\/tutorials\/?p=259"},"modified":"2016-02-29T09:51:56","modified_gmt":"2016-02-29T09:51:56","slug":"score-and-respawning","status":"publish","type":"post","link":"https:\/\/www.doofah.com\/tutorials\/networking\/score-and-respawning\/","title":{"rendered":"Unity 5 Network Tutorial Part 6 \u2013 Score and Respawning"},"content":{"rendered":"<p>Welcome to part 6 of my Unity 5 basic networking tutorial, in this part we will add scores and respawning.<\/p>\n<p><strong>Part6a &#8211; Death and respawning<\/strong><\/p>\n<p>In the last part of the tutorial, we left it where a player&#8217;s health reduced when hit by a laser shot, but it didn&#8217;t detect when it reached zero. So we need to address that now.<\/p>\n<p>Firstly open the <em>HealthAndDamage<\/em> script in your editor and change the replace the <em>TakeDamage<\/em>() function with the following new <em>TakeDamage<\/em>() function.<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/dmbpQjQF\"><\/script><br \/>\n What this does is prevent the health value from going below zero.<br \/>\n Then if it is zero it calls the <em>DoDeath<\/em> routine to take the appropriate action, which in this case will be to move the player to one of the spawn points and reset their health to 100.<\/p>\n<p>We need to add the <em>DoDeath<\/em>() function next, so add this code just below the <em>TakeDamage<\/em>() function.<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/cvidKP41\"><\/script><br \/>\nWe also need a new SyncVar to store the respawn point in, so add this code just below the health SyncVar.<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/9XT22zZc\"><\/script><\/p>\n<p>All the above bits of extra code do is call another two functions, which we will add next, one to obtain the new spawn point, which is then stored in the <em>respawnPos<\/em> syncvar and the other is an RPC to move the player to the respawn point.<br \/>\n We mark the <em>DoDeath<\/em>() function with the [Server] attribute as we only want respawning to be handled on the server.<\/p>\n<p>Add this code just below the <em>DoDeath<\/em>() function, this is what we will use to get a new spawn point, it makes use of the spawn points we set up in the <em>NetworkManager<\/em> previously.<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/6ujjXdYG\"><\/script><\/p>\n<p>Lastly we need to add the <em>HandlePlayerDeath<\/em>() function as so<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/zKsSNt88\"><\/script><\/p>\n<p>This is the function that does most of the work of respawning our player. Firstly we check <em>isLocalPlayer<\/em>, as we only want this to run on the local player object (it will automatically synchronize the changes to remote clients).<br \/>\n Then we reset our position to the <em>respawnPos<\/em> value obtained earlier, and finally we tell the server to set our health back to 100.<\/p>\n<p>This is the entire <em>HealthAndDamage<\/em> script as it now stands<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/wU0JVF1v\"><\/script><\/p>\n<p>Save this and build and run the game, you will see that when a player&#8217;s health reaches zero, it is instantly respawned in one of the spawn points and it&#8217;s health is restored to 100.<\/p>\n<p><strong>Part 6b &#8211; Scoring<\/strong><\/p>\n<p>The only thing left for this part of the tutorial is to handle scoring when a player gets a kill. So we&#8217;ll sort that out now.<\/p>\n<p>Firsly we need to update the HUD to give it a way of displaying our score. So open up the <em>Online Scene<\/em>, and with the HUD game object selected add a <em>Text<\/em> object (<strong>GameObject-&gt;UI-&gt;Text<\/strong>) and rename it <em>ScoreText<\/em>, then make the following changes to its properties:-<\/p>\n<ul>\n<li>Anchor = Bottom Left<\/li>\n<li>Position = 223, 35, 0<\/li>\n<li>Width = 410<\/li>\n<li>Height = 80<\/li>\n<li>Text = Score: 0<\/li>\n<li>Font Size = 64<\/li>\n<li>Colour = white<\/li>\n<\/ul>\n<p>Once you&#8217;ve done that your <em>Online<\/em> <em>scene<\/em> should now look like this<br \/>\n<a href=\"http:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText.png\" rel=\"attachment wp-att-269\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-269\" src=\"http:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText-1024x551.png\" alt=\"ScoreText\" width=\"654\" height=\"352\" srcset=\"https:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText-1024x551.png 1024w, https:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText-300x161.png 300w, https:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText-768x413.png 768w, https:\/\/www.doofah.com\/tutorials\/wp-content\/uploads\/2016\/02\/ScoreText.png 1600w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/a><br \/>\nNow we&#8217;ve got somewhere to display the score, we need to add some code to actually do that, but before we go on, save the scene with the above changes.<\/p>\n<p>With the scene saved, the next thing we need to do is add a reference to the <em>ScoreText<\/em> game object and a new function to the HUD script. So open the HUD script in your editor.<br \/>\nThen add this line just below the <em>healthText<\/em> variable<\/p>\n<p><strong><span style=\"color: #008000;\">Text<\/span> scoreText;<\/strong><\/p>\n<p>Now add this line to the <em>Awake<\/em>() function<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/80XKwU2z\"><\/script><\/p>\n<p>Now add the function that we can use to display the score for the player as it changes.<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/x7kZL5gc\"><\/script><\/p>\n<p>The entire HUD script should look like this now:-<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/2ygJA6Q8\"><\/script><\/p>\n<p>Next, open up the <em>HealthAndDamage<\/em> script in your editor and add the following line to <em>TakeDamage<\/em>() function:<\/p>\n<p><strong><span style=\"font-family: Calibri;\">fromPlayer.GetComponent&lt;<span style=\"color: #008000;\">PlayerShoot<\/span>&gt;().AddScore();<\/span><\/strong><\/p>\n<p>so the <em>TakeDamage<\/em>() function should now look like this:-<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/NAjMnLWr\"><\/script><br \/>\nThis calls the <em>AddScore<\/em> function on the player that fired the laser, if the targeted player&#8217;s health is zero, i.e. he is dead.<br \/>\nWe now need to make the <em>AddScore<\/em> function as it doesn&#8217;t actually exist yet. So open the <em>PlayerShoot<\/em> script in your editor and add the following code beneath the <em>CanFire<\/em>() function:-<\/p>\n<p><script src=\"\/\/pastebin.com\/embed_js\/cSyUdyDj\"><\/script><\/p>\n<p>We also need to declare a variable to store the player score in, so add this line just below the <em>nextFireTime<\/em> variable:-<\/p>\n<p><span style=\"font-family: Calibri;\"><strong><span style=\"color: #800080;\">int\u00a0<\/span>score;<\/strong><\/span><\/p>\n<p>We are only interested in keeping track of the player&#8217;s score on the local player, so the score variable doesn&#8217;t need to be a SyncVar.<br \/>\n The <em>AddScore<\/em> function has to run on the server, as it invokes the <em>RpcAddScore<\/em>() RPC function we just added.<\/p>\n<p>In the <em>RpcAddScore<\/em>() function we check it its running on the local player, as this is the only place we want to update the score, and if it is, we add 5 to the current score and then call the HUD.<em>DisplayeScore<\/em>() function we made earlier, to display the new score.<\/p>\n<p>This is the entire <em>PlayerShoot <\/em>script with the above changes implemented:-<br \/>\n<script src=\"\/\/pastebin.com\/embed_js\/9SEYLadf\"><\/script><\/p>\n<p>Save the <em>PlayerShoot <\/em>script and the HUD script, load the offline scene and then build and run the game. Now when you shoot and kill an opponent, you will see your score increase by 5 each time.<\/p>\n<p>That&#8217;s it for part 6, in the next part we&#8217;ll improve the respawn function to add some delay and add health pick up items.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to part 6 of my Unity 5 basic networking tutorial, in this part we will add scores and respawning. Part6a &#8211; Death and respawning In the last part of the tutorial, we left it where a player&#8217;s health reduced when hit by a laser shot, but it didn&#8217;t detect when it reached zero. So [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":14,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[11,12,13,16,9,15,10,14,8],"_links":{"self":[{"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/posts\/259"}],"collection":[{"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":27,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/posts\/259\/revisions\/287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/media\/14"}],"wp:attachment":[{"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doofah.com\/tutorials\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}