I have a code that moves a 2d sprite efficiently through a grid using Keyboard inputs.
it works, however, it uses around 300 lines of code. Is there a way to improve the code or at least better?
public enum PlayerDirection
{
facingUp = 1,
facingRight,
facingDown,
facingLeft,
}
public class PlayerController : MonoBehaviour
{
[SerializeField] float movementInputX;
[SerializeField] float movementInputY;
Vector2 movementX;
Vector2 movementY;
public float speed = 1;
private int lineDirectionValue = (int) PlayerDirection.facingDown;
public float gridSnap = 0.9f;
// Start is called before the first frame update+
private void Awake()
{
}
private void Start()
{
}
private void Update()
{
movementInputX = Input.GetAxisRaw("Horizontal");
movementInputY = Input.GetAxisRaw("Vertical");
movementX = new Vector2(movementInputX, 0);
movementY = new Vector2(0, movementInputY);
LineDirectionValue();
LineDirectionGizmo();
//GridCorrection();
}
private void FixedUpdate()
{
Movement();
IsOnHorizontalGrid();
IsOnVerticalGrid();
Debug.Log(Mathf.Abs(transform.position.x % 1) + "," + Mathf.Abs(transform.position.y % 1));
}
private void Movement()
{
Vector2 gridCorrection = transform.position;
if(IsFacingUp() && IsOnVerticalGrid())
{
if(movementInputY == 1 || movementInputX != 0)
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if(gridSnap < Mathf.Abs(gridCorrection.y % 1) && transform.position.y >= 0)
{
gridCorrection.y = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
else if((1 - gridSnap) > Mathf.Abs(gridCorrection.y % 1) && (gridCorrection.y % 1) != 0 && transform.position.y < 0)
{
gridCorrection.y = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
}
else if(IsFacingRight() && IsOnHorizontalGrid())
{
if(movementInputY != 0 || movementInputX == 1)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if(gridSnap < Mathf.Abs(gridCorrection.x % 1) && transform.position.x >= 0)
{
gridCorrection.x = Mathf.Round(gridCorrection.x);
transform.position = gridCorrection;
}
else if((1 - gridSnap) > Mathf.Abs(gridCorrection.x % 1) && (gridCorrection.x % 1) != 0 && transform.position.x < 0)
{
gridCorrection.x = Mathf.Round(gridCorrection.x);
transform.position = gridCorrection;
}
}
else if(IsFacingDown() && IsOnVerticalGrid())
{
if(movementInputY == -1 || movementInputX != 0)
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
if((1 - gridSnap) > Mathf.Abs(gridCorrection.y % 1) && (gridCorrection.y % 1) != 0 && transform.position.y >= 0)
{
gridCorrection.y = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
else if(gridSnap < Mathf.Abs(gridCorrection.y % 1) && transform.position.y < 0)
{
gridCorrection.y = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
}
else if(IsFacingLeft() && IsOnHorizontalGrid())
{
if(movementInputY != 0 || movementInputX == -1)
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
if((1 - gridSnap) > Mathf.Abs(gridCorrection.x % 1) && (gridCorrection.x % 1) != 0 && transform.position.x >= 0)
{
gridCorrection.x = Mathf.Round(gridCorrection.x);
transform.position = gridCorrection;
}
else if(gridSnap < Mathf.Abs(gridCorrection.x % 1) && transform.position.x < 0)
{
gridCorrection.x = Mathf.Round(gridCorrection.x);
transform.position = gridCorrection;
}
}
}
private void GridCorrection()
{
Vector2 gridCorrection = transform.position;
if(IsOnHorizontalGrid() == false)
{
gridCorrection.y = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
if(IsOnVerticalGrid() == false)
{
gridCorrection.x = Mathf.Round(gridCorrection.y);
transform.position = gridCorrection;
}
}
private bool IsOnVerticalGrid()
{
if(transform.position.x % 1 != 0)
{
//Debug.Log("Is not Y grid");
return false;
}
else
{
//Debug.Log(" Is on Y grid");
return true;
}
}
private bool IsOnHorizontalGrid()
{
if(transform.position.y % 1 != 0)
{
//Debug.Log("Is not X grid");
return false;
}
else
{
//Debug.Log(" Is on X grid");
return true;
}
}
private void LineDirectionValue()
{
switch(movementInputX, movementInputY)
{
case(0,1):
if(IsOnVerticalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingUp;
}
break;
case(1,1):
if(IsOnVerticalGrid())
{
lineDirectionValue = (int) PlayerDirection.facingUp;
}
else if(IsOnHorizontalGrid())
{
lineDirectionValue = (int) PlayerDirection.facingRight;
}
break;
case(1,0):
if(IsOnHorizontalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingRight;
}
break;
case(1,-1):
if(IsOnHorizontalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingRight;
}
else if(IsOnVerticalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingDown;
}
break;
case(0,-1):
if(IsOnVerticalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingDown;
}
break;
case(-1,-1):
if(IsOnHorizontalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingLeft;
}
else if(IsOnVerticalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingDown;
}
break;
case(-1,0):
if(IsOnHorizontalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingLeft;
}
break;
case(-1,1):
if(IsOnHorizontalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingLeft;
}
else if(IsOnVerticalGrid())
{
lineDirectionValue = (int)PlayerDirection.facingUp;
}
break;
}
}
private bool IsFacingUp()
{
if(IsOnVerticalGrid())
{
if(lineDirectionValue == (int)PlayerDirection.facingUp)
{
//Debug.Log("Is facing up");
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private bool IsFacingRight()
{
if(IsOnHorizontalGrid())
{
if(lineDirectionValue == (int)PlayerDirection.facingRight)
{
//Debug.Log("Is facing right");
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private bool IsFacingDown()
{
if(IsOnVerticalGrid())
{
if(lineDirectionValue == (int)PlayerDirection.facingDown)
{
//Debug.Log("Is facing down");
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private bool IsFacingLeft()
{
if(IsOnHorizontalGrid())
{
if(lineDirectionValue == (int)PlayerDirection.facingLeft)
{
//Debug.Log("Is facing left");
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private void LineDirectionGizmo()
{
if(IsFacingUp())
{
Debug.DrawRay(transform.position, Vector2.up);
}
else if(IsFacingRight())
{
Debug.DrawRay(transform.position, Vector2.right);
}
else if(IsFacingDown())
{
Debug.DrawRay(transform.position, Vector2.down);
}
else if(IsFacingLeft())
{
Debug.DrawRay(transform.position, Vector2.left);
}
}
}