Test de communication de paquets entre un client et un serveur
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Player.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Player : MonoBehaviour
  5. {
  6. public int id;
  7. public string username;
  8. public CharacterController controller;
  9. public Transform shootOrigin;
  10. public float gravity = -9.81f;
  11. public float moveSpeed = 5f;
  12. public float jumpSpeed = 5f;
  13. public float health;
  14. public float maxHealth = 100f;
  15. private float yVelocity = 0;
  16. private bool[] inputs;
  17. private void Start()
  18. {
  19. gravity *= Time.fixedDeltaTime * Time.fixedDeltaTime;
  20. moveSpeed *= Time.fixedDeltaTime;
  21. jumpSpeed *= Time.fixedDeltaTime;
  22. }
  23. public void Initialize(int _id, string _username)
  24. {
  25. id = _id;
  26. username = _username;
  27. health = maxHealth;
  28. inputs = new bool[5];
  29. }
  30. public void FixedUpdate()
  31. {
  32. if (health <= 0f)
  33. {
  34. return;
  35. }
  36. Vector2 _inputDirection = Vector2.zero;
  37. if (inputs[0])
  38. {
  39. _inputDirection.y += 1;
  40. }
  41. if (inputs[1])
  42. {
  43. _inputDirection.y -= 1;
  44. }
  45. if (inputs[2])
  46. {
  47. _inputDirection.x -= 1;
  48. }
  49. if (inputs[3])
  50. {
  51. _inputDirection.x += 1;
  52. }
  53. Move(_inputDirection);
  54. }
  55. private void Move(Vector2 _inputDirection)
  56. {
  57. Vector3 _moveDirection = transform.right * _inputDirection.x + transform.forward * _inputDirection.y;
  58. _moveDirection *= moveSpeed;
  59. if (controller.isGrounded)
  60. {
  61. yVelocity = 0f;
  62. if (inputs[4])
  63. {
  64. yVelocity = jumpSpeed;
  65. }
  66. }
  67. yVelocity += gravity;
  68. _moveDirection.y = yVelocity;
  69. controller.Move(_moveDirection);
  70. ServerSend.PlayerPosition(this);
  71. ServerSend.PlayerRotation(this);
  72. }
  73. public void SetInput(bool[] _inputs, Quaternion _rotation)
  74. {
  75. inputs = _inputs;
  76. transform.rotation = _rotation;
  77. }
  78. public void Shoot(Vector3 _viewDirection)
  79. {
  80. if (Physics.Raycast(shootOrigin.position, _viewDirection, out RaycastHit _hit, 25f))
  81. {
  82. if (_hit.collider.CompareTag("Player"))
  83. {
  84. _hit.collider.GetComponent<Player>().TakeDamage(50f);
  85. }
  86. }
  87. }
  88. public void TakeDamage(float _damage)
  89. {
  90. if (health <= 0f)
  91. {
  92. return;
  93. }
  94. health -= _damage;
  95. if (health <= 0f)
  96. {
  97. health = 0f;
  98. controller.enabled = false;
  99. transform.position = new Vector3(0f, 25f, 0f);
  100. ServerSend.PlayerPosition(this);
  101. StartCoroutine(Respawn());
  102. }
  103. ServerSend.PlayerHealth(this);
  104. }
  105. private IEnumerator Respawn()
  106. {
  107. yield return new WaitForSeconds(5f);
  108. health = maxHealth;
  109. controller.enabled = true;
  110. ServerSend.PlayerRespawned(this);
  111. }
  112. }