Created
November 10, 2024 15:11
-
-
Save vbayeva/088fe844ce706e175444aed17174ae9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GameController : MonoBehaviour | |
| { | |
| [SerializeField] private InputActionReference menuInput; | |
| [SerializeField] private InputActionReference exitMenuInput; | |
| [SerializeField] private UIController uiController; | |
| [SerializeField] private PlayerController playerController; | |
| private void OnEnable() | |
| { | |
| menuInput.action.performed += ShowUI; | |
| exitMenuInput.action.performed += HideUI; | |
| menuInput.action.Enable(); | |
| exitMenuInput.action.Disable(); | |
| } | |
| private void Start() | |
| { | |
| uiController.gameObject.SetActive(false); | |
| } | |
| private void OnDisable() | |
| { | |
| menuInput.action.performed -= ShowUI; | |
| exitMenuInput.action.performed -= HideUI; | |
| menuInput.action.Disable(); | |
| exitMenuInput.action.Disable(); | |
| } | |
| private void HideUI(InputAction.CallbackContext context) | |
| { | |
| uiController.gameObject.SetActive(false); | |
| playerController.EnableInput(); | |
| menuInput.action.Enable(); | |
| exitMenuInput.action.Disable(); | |
| } | |
| private void ShowUI(InputAction.CallbackContext context) | |
| { | |
| uiController.gameObject.SetActive(true); | |
| playerController.DisableInput(); | |
| menuInput.action.Disable(); | |
| exitMenuInput.action.Enable(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [RequireComponent(typeof(CharacterController))] | |
| public class PlayerController : MonoBehaviour | |
| { | |
| [SerializeField] private float speed = 5f; | |
| private CharacterController _characterController; | |
| private InputSystem_Actions _inputActions; | |
| private Vector2 _input; | |
| private void Awake() | |
| { | |
| _inputActions = new InputSystem_Actions(); | |
| _characterController = GetComponent<CharacterController>(); | |
| } | |
| private void OnEnable() | |
| { | |
| _inputActions.Player.Enable(); | |
| _inputActions.Player.Attack.performed += Attack; | |
| } | |
| private void OnDisable() | |
| { | |
| _inputActions.Player.Disable(); | |
| _inputActions.Player.Attack.performed -= Attack; | |
| } | |
| private void Update() | |
| { | |
| GatherInput(); | |
| Move(); | |
| } | |
| private void Attack(InputAction.CallbackContext context) | |
| { | |
| Debug.Log("Attacking!"); | |
| } | |
| private void Move() | |
| { | |
| Vector3 moveDireciton = transform.forward * speed * Time.deltaTime * _input.magnitude; | |
| _characterController.Move(moveDireciton); | |
| } | |
| private void GatherInput() | |
| { | |
| _input = _inputActions.Player.Move.ReadValue<Vector2>(); | |
| } | |
| public void EnableInput() | |
| { | |
| _inputActions.Player.Enable(); | |
| } | |
| public void DisableInput() | |
| { | |
| _inputActions.Player.Disable(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UIController : MonoBehaviour | |
| { | |
| [SerializeField] private InputActionReference clickReference; | |
| private void OnEnable() | |
| { | |
| clickReference.action.performed += HandleClick; | |
| } | |
| private void HandleClick(InputAction.CallbackContext context) | |
| { | |
| if (context.ReadValue<float>() == 1f) | |
| Debug.Log("Clicking!"); | |
| } | |
| private void OnDisable() | |
| { | |
| clickReference.action.performed -= HandleClick; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment