From 21de9bff1a1d9220f999883660a390dd1a5c9cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Wed, 29 May 2019 19:18:42 +0300 Subject: [PATCH] Add Player class and basic movement code --- Assets/Scripts/src/Base/GameplayComponent.cs | 9 ++++ Assets/Scripts/src/Base/PlayerBase.cs | 7 +++ Assets/Scripts/src/Player/Player.cs | 48 ++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Assets/Scripts/src/Base/GameplayComponent.cs create mode 100644 Assets/Scripts/src/Base/PlayerBase.cs create mode 100644 Assets/Scripts/src/Player/Player.cs diff --git a/Assets/Scripts/src/Base/GameplayComponent.cs b/Assets/Scripts/src/Base/GameplayComponent.cs new file mode 100644 index 0000000..a3c870f --- /dev/null +++ b/Assets/Scripts/src/Base/GameplayComponent.cs @@ -0,0 +1,9 @@ +using UnityEngine; + +namespace src.Base +{ + public abstract class GameplayComponent : MonoBehaviour + { + + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Base/PlayerBase.cs b/Assets/Scripts/src/Base/PlayerBase.cs new file mode 100644 index 0000000..401d623 --- /dev/null +++ b/Assets/Scripts/src/Base/PlayerBase.cs @@ -0,0 +1,7 @@ +namespace src.Base +{ + public abstract class PlayerBase : GameplayComponent + { + public float movementSpeed = 4f; + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Player/Player.cs b/Assets/Scripts/src/Player/Player.cs new file mode 100644 index 0000000..206adbd --- /dev/null +++ b/Assets/Scripts/src/Player/Player.cs @@ -0,0 +1,48 @@ +using System; +using src.Base; +using UnityEngine; + +namespace src.Player +{ + public class Player : PlayerBase + { + /* Movement */ + private Rigidbody2D _rigidbody2d; + + private void Start() + { + _rigidbody2d = GetComponent(); + } + + private void Update() + { + HandleMovement(); + } + + private void HandleMovement() + { +#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL + var horizontal = Input.GetAxis("Horizontal"); + var vertical = Input.GetAxis("Vertical"); + + // Restrict movement in only one axis at the same time. + if (Math.Abs(vertical) > 0.00001) + { + horizontal = 0; + } + else + { + vertical = 0; + } + + var movementVector = new Vector2(horizontal, vertical); + + _rigidbody2d.position += movementSpeed * Time.deltaTime * movementVector; +#elif UNITY_IOS || UNITY_ANDROID + // Phone movement is not supported yet. +#elif UNITY_PS4 || UNITY_XBOXONE + // Console movement is not supported yet. +#endif + } + } +} \ No newline at end of file