MoveByKeys.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="OnJoinedInstantiate.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities,
  4. // </copyright>
  5. // <summary>
  6. // Very basic component to move a GameObject by WASD and Space.
  7. // </summary>
  8. // <remarks>
  9. // Requires a PhotonView.
  10. // Disables itself on GameObjects that are not owned on Start.
  11. //
  12. // Speed affects movement-speed.
  13. // JumpForce defines how high the object "jumps".
  14. // JumpTimeout defines after how many seconds you can jump again.
  15. // </remarks>
  16. // <author>developer@exitgames.com</author>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. using UnityEngine;
  19. using Photon.Pun;
  20. using Photon.Realtime;
  21. namespace Photon.Pun.UtilityScripts
  22. {
  23. /// <summary>
  24. /// Very basic component to move a GameObject by WASD and Space.
  25. /// </summary>
  26. /// <remarks>
  27. /// Requires a PhotonView.
  28. /// Disables itself on GameObjects that are not owned on Start.
  29. ///
  30. /// Speed affects movement-speed.
  31. /// JumpForce defines how high the object "jumps".
  32. /// JumpTimeout defines after how many seconds you can jump again.
  33. /// </remarks>
  34. [RequireComponent(typeof(PhotonView))]
  35. public class MoveByKeys : Photon.Pun.MonoBehaviourPun
  36. {
  37. public float Speed = 10f;
  38. public float JumpForce = 200f;
  39. public float JumpTimeout = 0.5f;
  40. private bool isSprite;
  41. private float jumpingTime;
  42. private Rigidbody body;
  43. private Rigidbody2D body2d;
  44. public void Start()
  45. {
  46. //enabled = photonView.isMine;
  47. this.isSprite = (GetComponent<SpriteRenderer>() != null);
  48. this.body2d = GetComponent<Rigidbody2D>();
  49. this.body = GetComponent<Rigidbody>();
  50. }
  51. // Update is called once per frame
  52. public void FixedUpdate()
  53. {
  54. if (!photonView.IsMine)
  55. {
  56. return;
  57. }
  58. if ((Input.GetAxisRaw("Horizontal") < -0.1f) || (Input.GetAxisRaw("Horizontal") > 0.1f))
  59. {
  60. transform.position += Vector3.right * (Speed * Time.deltaTime) * Input.GetAxisRaw("Horizontal");
  61. }
  62. // jumping has a simple "cooldown" time but you could also jump in the air
  63. if (this.jumpingTime <= 0.0f)
  64. {
  65. if (this.body != null || this.body2d != null)
  66. {
  67. // obj has a Rigidbody and can jump (AddForce)
  68. if (Input.GetKey(KeyCode.Space))
  69. {
  70. this.jumpingTime = this.JumpTimeout;
  71. Vector2 jump = Vector2.up * this.JumpForce;
  72. if (this.body2d != null)
  73. {
  74. this.body2d.AddForce(jump);
  75. }
  76. else if (this.body != null)
  77. {
  78. this.body.AddForce(jump);
  79. }
  80. }
  81. }
  82. }
  83. else
  84. {
  85. this.jumpingTime -= Time.deltaTime;
  86. }
  87. // 2d objects can't be moved in 3d "forward"
  88. if (!this.isSprite)
  89. {
  90. if ((Input.GetAxisRaw("Vertical") < -0.1f) || (Input.GetAxisRaw("Vertical") > 0.1f))
  91. {
  92. transform.position += Vector3.forward * (Speed * Time.deltaTime) * Input.GetAxisRaw("Vertical");
  93. }
  94. }
  95. }
  96. }
  97. }