diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-07-02 08:46:23 -0700 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-07-02 08:46:23 -0700 |
commit | 8263edd59284aba390aca011d25b79efecef4c48 (patch) | |
tree | 6346e2afaaabd32156601cafaf20d4ee813befaf /Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs |
Diffstat (limited to 'Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs')
-rw-r--r-- | Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs b/Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs new file mode 100644 index 0000000..5b589e6 --- /dev/null +++ b/Assets/Samples/Input System/1.14.0/Simple Demo/SimpleController_UsingPlayerInput.cs @@ -0,0 +1,113 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEngine.InputSystem.Interactions; + +// Use a separate PlayerInput component for setting up input. +public class SimpleController_UsingPlayerInput : MonoBehaviour +{ + public float moveSpeed; + public float rotateSpeed; + public float burstSpeed; + public GameObject projectile; + + private bool m_Charging; + private Vector2 m_Rotation; + private Vector2 m_Look; + private Vector2 m_Move; + + public void OnMove(InputAction.CallbackContext context) + { + m_Move = context.ReadValue<Vector2>(); + } + + public void OnLook(InputAction.CallbackContext context) + { + m_Look = context.ReadValue<Vector2>(); + } + + public void OnFire(InputAction.CallbackContext context) + { + switch (context.phase) + { + case InputActionPhase.Performed: + if (context.interaction is SlowTapInteraction) + { + StartCoroutine(BurstFire((int)(context.duration * burstSpeed))); + } + else + { + Fire(); + } + m_Charging = false; + break; + + case InputActionPhase.Started: + if (context.interaction is SlowTapInteraction) + m_Charging = true; + break; + + case InputActionPhase.Canceled: + m_Charging = false; + break; + } + } + + public void OnGUI() + { + if (m_Charging) + GUI.Label(new Rect(100, 100, 200, 100), "Charging..."); + } + + public void Update() + { + // Update orientation first, then move. Otherwise move orientation will lag + // behind by one frame. + Look(m_Look); + Move(m_Move); + } + + private void Move(Vector2 direction) + { + if (direction.sqrMagnitude < 0.01) + return; + var scaledMoveSpeed = moveSpeed * Time.deltaTime; + // For simplicity's sake, we just keep movement in a single plane here. Rotate + // direction according to world Y rotation of player. + var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y); + transform.position += move * scaledMoveSpeed; + } + + private void Look(Vector2 rotate) + { + if (rotate.sqrMagnitude < 0.01) + return; + var scaledRotateSpeed = rotateSpeed * Time.deltaTime; + m_Rotation.y += rotate.x * scaledRotateSpeed; + m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89); + transform.localEulerAngles = m_Rotation; + } + + private IEnumerator BurstFire(int burstAmount) + { + for (var i = 0; i < burstAmount; ++i) + { + Fire(); + yield return new WaitForSeconds(0.1f); + } + } + + private void Fire() + { + var transform = this.transform; + var newProjectile = Instantiate(projectile); + newProjectile.transform.position = transform.position + transform.forward * 0.6f; + newProjectile.transform.rotation = transform.rotation; + const int size = 1; + newProjectile.transform.localScale *= size; + newProjectile.GetComponent<Rigidbody>().mass = Mathf.Pow(size, 3); + newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * 20f, ForceMode.Impulse); + newProjectile.GetComponent<MeshRenderer>().material.color = + new Color(Random.value, Random.value, Random.value, 1.0f); + } +} |