Add Player class and basic movement code

This commit is contained in:
Denis-Cosmin Nutiu 2019-05-29 19:18:42 +03:00
parent b9f78d1208
commit 21de9bff1a
3 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,9 @@
using UnityEngine;
namespace src.Base
{
public abstract class GameplayComponent : MonoBehaviour
{
}
}

View file

@ -0,0 +1,7 @@
namespace src.Base
{
public abstract class PlayerBase : GameplayComponent
{
public float movementSpeed = 4f;
}
}

View file

@ -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<Rigidbody2D>();
}
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
}
}
}