SimpleController_UsingActionAsset.cs 3.4 KB

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