Advertisement
Guest User

Untitled

a guest
Aug 17th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Netcode;
  5. using TMPro;
  6. using LootLocker.Requests;
  7. using UnityEngine.UI;
  8.  
  9. public class KillHealthManager : NetworkBehaviour
  10. {
  11. // Max amount of health for each player
  12. public int maxHealth = 120;
  13.  
  14. // Range of the map for the player to spawn in
  15. [SerializeField] private float positionRange = 70f;
  16.  
  17. // How many kills a player needs to win the game
  18. public int killsToWin = 20;
  19.  
  20. // Textbox for the text that shows who won the round
  21. public TMP_Text winText;
  22.  
  23. // Keeps track of how many kills this player has
  24. private int playerKills = 0;
  25.  
  26. // The name of the player
  27. private string playerName;
  28.  
  29. private void Update()
  30. {
  31. CheckHealth();
  32. }
  33.  
  34. // Just checks the players health and kills them if its 0 or below
  35. public void CheckHealth()
  36. {
  37. if (health.Value <= 0)
  38. {
  39. Die();
  40. }
  41. }
  42.  
  43. // This is the health variable that is synced across the network
  44. [HideInInspector] public NetworkVariable<int> health = new NetworkVariable<int>(
  45. value: 120,
  46. NetworkVariableReadPermission.Everyone,
  47. NetworkVariableWritePermission.Owner);
  48.  
  49. public void OnTriggerEnter(Collider collider)
  50. {
  51. //Checks if the player has been hit by a object and checks if that object had the script projectile, signifying they have been hit by a bullet and will now be subracted 15 health.
  52. if (collider.GetComponent<Projectile>())
  53. {
  54. Debug.Log("Player Hit");
  55.  
  56. // Negates their health by 15.
  57. health.Value -= 15;
  58. }
  59. }
  60.  
  61. // The script that gets called when a player dies/health is under 0.
  62. void Die()
  63. {
  64. //When a players health is 0 or under, this script will run which resets their health and teleports them to a random position
  65. transform.position = new Vector3(Random.Range(positionRange, -positionRange), 0,Random.Range(positionRange, -positionRange));
  66. Debug.Log("Player Died");
  67. health.Value = 120;
  68. }
  69.  
  70. private void Start()
  71. {
  72. // This is just a service I am using to keep track of player names and the items they own. Dont worry about this. All its doing is getting the player name from Loot Locker and setting it as the player name variable.
  73. LootLockerSDKManager.GetPlayerName((response) =>
  74. {
  75. if (response.success)
  76. {
  77. Debug.Log("Successfully retrieved player name: " + response.name);
  78. playerName = response.name;
  79. }
  80. else
  81. {
  82. Debug.Log("Error getting player name");
  83. }
  84. });
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement