Add Player class and basic movement code
This commit is contained in:
parent
b9f78d1208
commit
21de9bff1a
3 changed files with 64 additions and 0 deletions
9
Assets/Scripts/src/Base/GameplayComponent.cs
Normal file
9
Assets/Scripts/src/Base/GameplayComponent.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace src.Base
|
||||
{
|
||||
public abstract class GameplayComponent : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
}
|
7
Assets/Scripts/src/Base/PlayerBase.cs
Normal file
7
Assets/Scripts/src/Base/PlayerBase.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace src.Base
|
||||
{
|
||||
public abstract class PlayerBase : GameplayComponent
|
||||
{
|
||||
public float movementSpeed = 4f;
|
||||
}
|
||||
}
|
48
Assets/Scripts/src/Player/Player.cs
Normal file
48
Assets/Scripts/src/Player/Player.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue