SimpleController_UsingActions.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem.Interactions;
  5. // Using simple actions with callbacks.
  6. public class SimpleController_UsingActions : MonoBehaviour
  7. {
  8. public float moveSpeed;
  9. public float rotateSpeed;
  10. public float burstSpeed;
  11. public GameObject projectile;
  12. public InputAction moveAction;
  13. public InputAction lookAction;
  14. public InputAction fireAction;
  15. private bool m_Charging;
  16. private Vector2 m_Rotation;
  17. public void Awake()
  18. {
  19. // We could use `fireAction.triggered` in Update() but that makes it more difficult to
  20. // implement the charging mechanism. So instead we use the `started`, `performed`, and
  21. // `canceled` callbacks to run the firing logic right from within the action.
  22. fireAction.performed +=
  23. ctx =>
  24. {
  25. if (ctx.interaction is SlowTapInteraction)
  26. {
  27. StartCoroutine(BurstFire((int)(ctx.duration * burstSpeed)));
  28. }
  29. else
  30. {
  31. Fire();
  32. }
  33. m_Charging = false;
  34. };
  35. fireAction.started +=
  36. ctx =>
  37. {
  38. if (ctx.interaction is SlowTapInteraction)
  39. m_Charging = true;
  40. };
  41. fireAction.canceled +=
  42. ctx =>
  43. {
  44. m_Charging = false;
  45. };
  46. }
  47. public void OnEnable()
  48. {
  49. moveAction.Enable();
  50. lookAction.Enable();
  51. fireAction.Enable();
  52. }
  53. public void OnDisable()
  54. {
  55. moveAction.Disable();
  56. lookAction.Disable();
  57. fireAction.Disable();
  58. }
  59. public void OnGUI()
  60. {
  61. if (m_Charging)
  62. GUI.Label(new Rect(100, 100, 200, 100), "Charging...");
  63. }
  64. public void Update()
  65. {
  66. var look = lookAction.ReadValue<Vector2>();
  67. var move = moveAction.ReadValue<Vector2>();
  68. // Update orientation first, then move. Otherwise move orientation will lag
  69. // behind by one frame.
  70. Look(look);
  71. Move(move);
  72. }
  73. private void Move(Vector2 direction)
  74. {
  75. if (direction.sqrMagnitude < 0.01)
  76. return;
  77. var scaledMoveSpeed = moveSpeed * Time.deltaTime;
  78. // For simplicity's sake, we just keep movement in a single plane here. Rotate
  79. // direction according to world Y rotation of player.
  80. var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y);
  81. transform.position += move * scaledMoveSpeed;
  82. }
  83. private void Look(Vector2 rotate)
  84. {
  85. if (rotate.sqrMagnitude < 0.01)
  86. return;
  87. var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
  88. m_Rotation.y += rotate.x * scaledRotateSpeed;
  89. m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
  90. transform.localEulerAngles = m_Rotation;
  91. }
  92. private IEnumerator BurstFire(int burstAmount)
  93. {
  94. for (var i = 0; i < burstAmount; ++i)
  95. {
  96. Fire();
  97. yield return new WaitForSeconds(0.1f);
  98. }
  99. }
  100. private void Fire()
  101. {
  102. var transform = this.transform;
  103. var newProjectile = Instantiate(projectile);
  104. newProjectile.transform.position = transform.position + transform.forward * 0.6f;
  105. newProjectile.transform.rotation = transform.rotation;
  106. var size = 1;
  107. newProjectile.transform.localScale *= size;
  108. newProjectile.GetComponent<Rigidbody>().mass = Mathf.Pow(size, 3);
  109. newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * 20f, ForceMode.Impulse);
  110. newProjectile.GetComponent<MeshRenderer>().material.color =
  111. new Color(Random.value, Random.value, Random.value, 1.0f);
  112. }
  113. }