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