暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleController_UsingPlayerInput.cs 3.5KB

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