diff --git a/Assets/Scripts/GameInput.cs b/Assets/Scripts/GameInput.cs new file mode 100644 index 0000000..d8bbc9a --- /dev/null +++ b/Assets/Scripts/GameInput.cs @@ -0,0 +1,32 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GameInput : MonoBehaviour +{ + public Vector2 GetMovementVectorNormalized() + { + Vector2 inputVector = new Vector2(0, 0); + + if (Input.GetKey(KeyCode.W)) + { + inputVector.y = +1; + } + if (Input.GetKey(KeyCode.S)) + { + inputVector.y = -1; + } + if (Input.GetKey(KeyCode.A)) + { + inputVector.x = -1; + } + if (Input.GetKey(KeyCode.D)) + { + inputVector.x = +1; + } + + inputVector = inputVector.normalized; + + return inputVector; + } +} diff --git a/Assets/Scripts/GameInput.cs.meta b/Assets/Scripts/GameInput.cs.meta new file mode 100644 index 0000000..f7da380 --- /dev/null +++ b/Assets/Scripts/GameInput.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 413070e64894f014a8dbfe751be0534c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 17500fd..7eed1ca 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -6,31 +6,13 @@ public class Player : MonoBehaviour { [SerializeField] private float moveSpeed = 7f; + [SerializeField] private GameInput gameInput; private bool isWalking; private void Update() { - Vector2 inputVector = new Vector2(0,0); - - if (Input.GetKey(KeyCode.W)) - { - inputVector.y = +1; - } - if (Input.GetKey(KeyCode.S)) - { - inputVector.y = -1; - } - if (Input.GetKey(KeyCode.A)) - { - inputVector.x = -1; - } - if (Input.GetKey(KeyCode.D)) - { - inputVector.x = +1; - } - - inputVector = inputVector.normalized; + Vector2 inputVector = gameInput.GetMovementVectorNormalized(); Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y); transform.position += moveDir * moveSpeed * Time.deltaTime;