A game about forced loneliness, made by TACStudios
at master 80 lines 2.7 kB view raw
1#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS 2 3namespace UnityEngine.InputSystem.Samples.ProjectWideActions 4{ 5 public class ProjectWideActionsExample : MonoBehaviour 6 { 7 [SerializeField] public GameObject cube; 8 9 InputAction move; 10 InputAction look; 11 InputAction attack; 12 InputAction jump; 13 InputAction interact; 14 InputAction next; 15 InputAction previous; 16 InputAction sprint; 17 InputAction crouch; 18 19 // Start is called before the first frame update 20 void Start() 21 { 22 // Project-Wide Actions 23 if (InputSystem.actions) 24 { 25 move = InputSystem.actions.FindAction("Player/Move"); 26 look = InputSystem.actions.FindAction("Player/Look"); 27 attack = InputSystem.actions.FindAction("Player/Attack"); 28 jump = InputSystem.actions.FindAction("Player/Jump"); 29 interact = InputSystem.actions.FindAction("Player/Interact"); 30 next = InputSystem.actions.FindAction("Player/Next"); 31 previous = InputSystem.actions.FindAction("Player/Previous"); 32 sprint = InputSystem.actions.FindAction("Player/Sprint"); 33 crouch = InputSystem.actions.FindAction("Player/Crouch"); 34 } 35 else 36 { 37 Debug.Log("Setup Project Wide Input Actions in the Player Settings, Input System section"); 38 } 39 40 // Handle input by responding to callbacks 41 if (attack != null) 42 { 43 attack.performed += OnAttack; 44 attack.canceled += OnCancel; 45 } 46 } 47 48 private void OnAttack(InputAction.CallbackContext ctx) 49 { 50 cube.GetComponent<Renderer>().material.color = Color.red; 51 } 52 53 private void OnCancel(InputAction.CallbackContext ctx) 54 { 55 cube.GetComponent<Renderer>().material.color = Color.green; 56 } 57 58 void OnDestroy() 59 { 60 if (attack != null) 61 { 62 attack.performed -= OnAttack; 63 attack.canceled -= OnCancel; 64 } 65 } 66 67 // Update is called once per frame 68 void Update() 69 { 70 // Handle input by polling each frame 71 if (move != null) 72 { 73 var moveVal = move.ReadValue<Vector2>() * 10.0f * Time.deltaTime; 74 cube.transform.Translate(new Vector3(moveVal.x, moveVal.y, 0)); 75 } 76 } 77 } // class ProjectWideActionsExample 78} // namespace UnityEngine.InputSystem.Samples.ProjectWideActions 79 80#endif