A game about forced loneliness, made by TACStudios
1using System.Collections;
2using UnityEngine;
3using UnityEngine.InputSystem.Interactions;
4
5// Use action set asset instead of lose InputActions directly on component.
6public 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 var move = Quaternion.Euler(0, transform.eulerAngles.y, 0) * new Vector3(direction.x, 0, direction.y);
82 transform.position += move * scaledMoveSpeed;
83 }
84
85 private void Look(Vector2 rotate)
86 {
87 if (rotate.sqrMagnitude < 0.01)
88 return;
89 var scaledRotateSpeed = rotateSpeed * Time.deltaTime;
90 m_Rotation.y += rotate.x * scaledRotateSpeed;
91 m_Rotation.x = Mathf.Clamp(m_Rotation.x - rotate.y * scaledRotateSpeed, -89, 89);
92 transform.localEulerAngles = m_Rotation;
93 }
94
95 private IEnumerator BurstFire(int burstAmount)
96 {
97 for (var i = 0; i < burstAmount; ++i)
98 {
99 Fire();
100 yield return new WaitForSeconds(0.1f);
101 }
102 }
103
104 private void Fire()
105 {
106 var transform = this.transform;
107 var newProjectile = Instantiate(projectile);
108 newProjectile.transform.position = transform.position + transform.forward * 0.6f;
109 newProjectile.transform.rotation = transform.rotation;
110 const int size = 1;
111 newProjectile.transform.localScale *= size;
112 newProjectile.GetComponent<Rigidbody>().mass = Mathf.Pow(size, 3);
113 newProjectile.GetComponent<Rigidbody>().AddForce(transform.forward * 20f, ForceMode.Impulse);
114 newProjectile.GetComponent<MeshRenderer>().material.color =
115 new Color(Random.value, Random.value, Random.value, 1.0f);
116 }
117}