Improve Player Movement

This commit is contained in:
Denis-Cosmin Nutiu 2019-06-15 13:14:55 +03:00
parent c813d46662
commit 2e10cc44ec
2 changed files with 9 additions and 20 deletions

View file

@ -9,11 +9,11 @@ namespace src.Base
public float movementSpeed = 4f; public float movementSpeed = 4f;
/* Movement */ /* Movement */
protected Rigidbody2D Rigidbody2d; protected Rigidbody2D rigidbody2d;
protected void Start() protected void Start()
{ {
Rigidbody2d = GetComponent<Rigidbody2D>(); rigidbody2d = GetComponent<Rigidbody2D>();
} }
public void OnTriggerEnter2D(Collider2D other) public void OnTriggerEnter2D(Collider2D other)

View file

@ -7,12 +7,14 @@ namespace src.Player
{ {
public class PlayerController : PlayerBase public class PlayerController : PlayerBase
{ {
public Transform respawnPosition; private Transform _respawnPosition;
private BombsSpawner _bombsSpawner;
protected new void Start() protected new void Start()
{ {
base.Start(); base.Start();
_respawnPosition = GameObject.Find("RespawnPosition").transform;
_bombsSpawner = GameObject.Find("BombSpawner").GetComponent<BombsSpawner>();
/* Always start at the starting point. */ /* Always start at the starting point. */
Respawn(); Respawn();
} }
@ -32,20 +34,8 @@ namespace src.Player
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
var horizontal = Input.GetAxis("Horizontal"); var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical"); 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); var movementVector = new Vector2(horizontal, vertical);
rigidbody2d.MovePosition(rigidbody2d.position + movementSpeed * Time.deltaTime * movementVector);
Rigidbody2d.position += movementSpeed * Time.deltaTime * movementVector;
#elif UNITY_IOS || UNITY_ANDROID #elif UNITY_IOS || UNITY_ANDROID
// Phone movement is not supported yet. // Phone movement is not supported yet.
#elif UNITY_PS4 || UNITY_XBOXONE #elif UNITY_PS4 || UNITY_XBOXONE
@ -55,8 +45,7 @@ namespace src.Player
private void PlaceBomb() private void PlaceBomb()
{ {
GameObject bombsSpawnerObject = GameObject.Find("BombSpawner"); _bombsSpawner.PlaceBomb(transform);
bombsSpawnerObject.GetComponent<BombsSpawner>().PlaceBomb(transform);
} }
private void HandleBomb() private void HandleBomb()
@ -75,7 +64,7 @@ namespace src.Player
private void Respawn() private void Respawn()
{ {
transform.position = respawnPosition.position; transform.SetPositionAndRotation(_respawnPosition.position, Quaternion.identity);
} }
public void OnTriggerExit2D(Collider2D other) public void OnTriggerExit2D(Collider2D other)