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