I Initially did a grid based movement for a game I am working on using If-else Statement.
The source code is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum GRIDY { left, mid, right, topleft, topmid, topright, botleft, botmid, botright }
public class PlayerMove : MonoBehaviour
{
public GRIDY pos = GRIDY.mid;
float newXpos = 0f ,newYpos = 0f; //New Position of grid
bool Moveleft, Moveright, Moveup, Movedown;
[SerializeField] float xvalue=3f;
[SerializeField] float yvalue=2f;
float x;
float y;
[SerializeField] float gridMoveSpeed = 1.5f; //Grid to Grid Speed
[SerializeField] float flightSpeed = 400f; //FlightSpeed
CharacterController controller;
Rigidbody rb;
//float FSpeed = 5f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
controller = GetComponent();
//transform.position = Vector3.zero;
//bool movingUp , movingDown, movingLeft, movingRight = false; //Touch
}
// Update is called once per frame
void Update()
{
Moveleft = Input.GetKeyDown(KeyCode.A);
Moveright = Input.GetKeyDown(KeyCode.D);
Moveup =Input.GetKeyDown(KeyCode.W);
Movedown =Input.GetKeyDown(KeyCode.S);
if(Moveleft)
{
if (pos == GRIDY.mid)
{
newXpos = -xvalue;
pos = GRIDY.left;
}
else if (pos == GRIDY.topmid)
{
newXpos = -xvalue;
pos = GRIDY.topleft;
}
else if (pos == GRIDY.botmid)
{
newXpos = -xvalue;
pos = GRIDY.botleft;
}
else if (pos == GRIDY.right)
{
newXpos = 0;
pos = GRIDY.mid;
}
else if (pos == GRIDY.topright)
{
newXpos = 0;
pos = GRIDY.topmid;
}
else if (pos == GRIDY.botright)
{
newXpos = 0;
pos = GRIDY.botmid;
}
}
if (Moveright)
{
if (pos == GRIDY.mid)
{
newXpos = xvalue;
pos = GRIDY.right;
}
else if (pos == GRIDY.topmid)
{
newXpos = xvalue;
pos = GRIDY.topright;
}
else if (pos == GRIDY.botmid)
{
newXpos = xvalue;
pos = GRIDY.botright;
}
else if (pos == GRIDY.left)
{
newXpos = 0;
pos = GRIDY.mid;
}
else if (pos == GRIDY.topleft)
{
newXpos = 0;
pos = GRIDY.topmid;
}
else if (pos == GRIDY.botleft)
{
newXpos = 0;
pos = GRIDY.botmid;
}
}
if (Moveup)
{
if (pos == GRIDY.mid)
{
newYpos = yvalue;
pos = GRIDY.topmid;
}
else if (pos == GRIDY.botmid)
{
newYpos = 0;
pos = GRIDY.mid;
}
else if (pos == GRIDY.left)
{
newYpos = yvalue;
pos = GRIDY.topleft;
}
else if (pos == GRIDY.botleft)
{
newYpos = 0;
pos = GRIDY.left;
}
else if (pos == GRIDY.right)
{
newYpos = yvalue;
pos = GRIDY.topright;
}
else if (pos == GRIDY.botright)
{
newYpos = 0;
pos = GRIDY.right;
}
}
if (Movedown)
{
if (pos == GRIDY.mid)
{
newYpos = -yvalue;
pos = GRIDY.botmid;
}
else if (pos == GRIDY.topmid)
{
newYpos = 0;
pos = GRIDY.mid;
}
else if (pos == GRIDY.left)
{
newYpos = -yvalue;
pos = GRIDY.botleft;
}
else if (pos == GRIDY.topleft)
{
newYpos = 0;
pos = GRIDY.left;
}
else if(pos==GRIDY.right)
{
newYpos = -yvalue;
pos = GRIDY.botright;
}
else if (pos == GRIDY.topright)
{
newYpos = 0;
pos = GRIDY.right;
}
}
x = Mathf.Lerp(x, newXpos, Time.deltaTime * gridMoveSpeed);
y = Mathf.Lerp(y, newYpos, Time.deltaTime * gridMoveSpeed);
controller.Move((x - transform.position.x) * Vector3.right * gridMoveSpeed);
controller.Move((y - transform.position.y) * Vector3.up * gridMoveSpeed);
rb.velocity = transform.forward * flightSpeed * Time.deltaTime;
}
}
I wanna know is there any other way I can try and achieve this 3x3 matric grid based movement and encapsulate my code. Thanks in advance for the help.