Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. Codigo del Objeto "Cliente":
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.Networking;
  8. public class PlayerObejct : NetworkBehaviour {
  9.  
  10. public GameObject playerPrefab;
  11. GameObject myPlayerUnit;
  12. [SyncVar(hook = "OnPlayerNameChanged")]
  13. public string playerName = "Anonymous";
  14. public Vector3 spawnPosition;
  15. public Image hpBar;
  16.  
  17. // Use this for initialization
  18. void Start () {
  19. if (!isLocalPlayer)
  20. return;
  21. CmdCheckPlayer();
  22. }
  23.  
  24. // Update is called once per frame
  25. void Update () {
  26. if (!isLocalPlayer)
  27. return;
  28. if (gameObject.name != playerName)
  29. {
  30. gameObject.name = playerName;
  31. }
  32.  
  33. }
  34.  
  35. void OnPlayerNameChanged(string newName)
  36. {
  37. gameObject.name = newName;
  38. playerName = newName;
  39. }
  40.  
  41. [Command]
  42. void CmdSpawnMyUnit(Vector3 t)
  43. {
  44.  
  45. GameObject go = Instantiate(playerPrefab);
  46. myPlayerUnit = go;
  47. myPlayerUnit.transform.position = t;
  48. myPlayerUnit.GetComponent<PlayerUnit>().creatorName=gameObject.name;
  49. NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
  50.  
  51. }
  52.  
  53. [Command]
  54. void CmdChangePlayerName(string n)
  55. {
  56. playerName = n;
  57.  
  58. }
  59.  
  60. [Command]
  61. void CmdCheckPlayer()
  62. {
  63. if (!GameObject.Find("Player1"))
  64. {
  65. CmdChangePlayerName("Player1");
  66. spawnPosition = new Vector3(4.27f, 0, -4.51f);
  67. hpBar = GameObject.Find("Player1Bar").GetComponent<Image>();
  68. }
  69. else
  70. {
  71. CmdChangePlayerName("Player2");
  72. spawnPosition = new Vector3(-4.07f, 0, -4.51f);
  73. hpBar = GameObject.Find("Player2Bar").GetComponent<Image>();
  74. }
  75. CmdSpawnMyUnit(spawnPosition);
  76. }
  77.  
  78. }
  79.  
  80. Codigo de la Instancia del "Cliente":
  81.  
  82. using System.Collections;
  83. using System.Collections.Generic;
  84. using UnityEngine;
  85. using UnityEngine.Networking;
  86.  
  87. public class PlayerUnit : NetworkBehaviour {
  88.  
  89. public string creatorName;
  90.  
  91. // Use this for initialization
  92. void Start () {
  93.  
  94. }
  95.  
  96. // Update is called once per frame
  97. void Update () {
  98.  
  99. if (!hasAuthority)
  100. {
  101. return;
  102. }
  103.  
  104. if (Input.GetKeyDown(KeyCode.Space))
  105. {
  106.  
  107. }
  108.  
  109. }
  110.  
  111. [Command]
  112. void CmdDealDmg()
  113. {
  114. // Funcion para hacer daño al otro;
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement