CouchPlayer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Mirror.Examples.CouchCoop
  4. {
  5. public class CouchPlayer : NetworkBehaviour
  6. {
  7. public Rigidbody rb;
  8. public float movementSpeed = 3;
  9. public float jumpSpeed = 6;
  10. private float movementVelocity;
  11. private bool isGrounded;
  12. public CouchPlayerManager couchPlayerManager;
  13. private KeyCode jumpKey = KeyCode.Space; // Check CouchPlayerManager for controls
  14. private KeyCode leftKey = KeyCode.LeftArrow;
  15. private KeyCode rightKey = KeyCode.RightArrow;
  16. [SyncVar(hook = nameof(OnNumberChangedHook))]
  17. public int playerNumber = 0;
  18. public Text textPlayerNumber;
  19. public override void OnStartAuthority()
  20. {
  21. this.enabled = true;
  22. if (isOwned)
  23. {
  24. #if UNITY_2021_3_OR_NEWER
  25. couchPlayerManager = GameObject.FindAnyObjectByType<CouchPlayerManager>();
  26. #else
  27. // Deprecated in Unity 2023.1
  28. couchPlayerManager = GameObject.FindObjectOfType<CouchPlayerManager>();
  29. #endif
  30. // setup controls according to the pre-sets on CouchPlayerManager
  31. jumpKey = couchPlayerManager.playerKeyJump[playerNumber];
  32. leftKey = couchPlayerManager.playerKeyLeft[playerNumber];
  33. rightKey = couchPlayerManager.playerKeyRight[playerNumber];
  34. }
  35. }
  36. public void Start()
  37. {
  38. SetPlayerUI();
  39. }
  40. void Update()
  41. {
  42. if (!Application.isFocused) return;
  43. if (isOwned == false) { return; }
  44. // you can control all local players via arrow keys and space bar for fun testing
  45. // otherwise check and set individual controls in CouchPlayerManager script.
  46. if (isGrounded == true)
  47. {
  48. if (Input.GetKey(KeyCode.Space) || Input.GetKeyDown(jumpKey))
  49. {
  50. rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
  51. }
  52. }
  53. movementVelocity = 0;
  54. if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(leftKey))
  55. {
  56. movementVelocity = -movementSpeed;
  57. }
  58. if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(rightKey))
  59. {
  60. movementVelocity = movementSpeed;
  61. }
  62. rb.velocity = new Vector2(movementVelocity, rb.velocity.y);
  63. }
  64. [ClientCallback]
  65. void OnCollisionExit(Collision col)
  66. {
  67. if (isOwned == false) { return; }
  68. isGrounded = false;
  69. }
  70. [ClientCallback]
  71. void OnCollisionStay(Collision col)
  72. {
  73. if (isOwned == false) { return; }
  74. isGrounded = true;
  75. }
  76. void OnNumberChangedHook(int _old, int _new)
  77. {
  78. //Debug.Log(name + " - OnNumberChangedHook: " + playerNumber);
  79. SetPlayerUI();
  80. }
  81. public void SetPlayerUI()
  82. {
  83. // called from hook and in start, to solve a race condition
  84. if (isOwned)
  85. {
  86. textPlayerNumber.text = "Local: " + playerNumber;
  87. }
  88. else
  89. {
  90. textPlayerNumber.text = "Remote: " + playerNumber;
  91. }
  92. }
  93. }
  94. }