Moving Character with recorded move, based on grid movement

as a title, i want a move my character, based on recorded move on another script,
so there is a Move.cs for record the move and Character.cs for moving the character.
but i dont understand how to move character. and what step should i do to make it works.

here is some example :

some of my Move.cs

public class Move : MonoBehaviour {
    private string[] allMoves;
    private bool empty;
    private int totalMoves;
    private int i=0;
    public string firstMove; // this first Move the one

    // Use this for initialization
    void Start()
    {
        totalMoves = Object.Length; // this Object reference from other script object, so ignore it
        allMoves = new string[totalMoves];
    }

    public void UpMoves()
    {
        AddMoves("up");
    }

    public void BotMoves()
    {
        AddMoves("bot");
    }

    public void LeftMoves()
    {
        AddMoves("left");
    }

    public void RightMoves()
    {
        AddMoves("right");
 
    }

    void AddMoves(string move)
    {
        if (empty) {
            firstMove = move;
            empty = false;
        }
        else
        {
            if (i < totalMoves)
            {
                allMoves[i] = move;
                i++;
            }
        }
    }

    public void NextMove()
    {
        string temp;
        firstMove = allMoves[0];
        for (int a = 0; a< totalMoves; a++){
            temp = allMoves[a + 1];
            allMoves[a] = temp;
        }
    }
 
}

and how my character could move a tile by tile by those recoded move?
i reference this code from another thread, have been modifed but i know it wont work

Character.cs

{
public float speed = 2.5f;
bool canMove;
Move move;
string movement;
Vector3 pos;
Transform tr;

void Start()
{
    move = FindObjectOfType<Move>();
    movement = move.firstMove;
    bool canMove = true;

}

void Go()
{
    if (move == "up")
    {
        transform.Translate(Vector3.up * speed * Time.deltaTime);
    }
    if (move == "left")
    {
        transform.Translate(Vector3.left * speed * Time.deltaTime);
    }
    if (move == "down")
    {
        transform.Translate(Vector3.down * speed * Time.deltaTime);
    }
    if (move == "right")
    {
        transform.Translate(Vector3.right * speed * Time.deltaTime);
    }

}

void FixedUpdate()
{
    if (canMove = true)
    {
        Go();
    }

}
}

You could set it up so the playback of each move lerps towards the destination grid spot. Once reached, you retrieve the next move (if there is one), and repeat the process.

That would be a little more intuitive than translating, as the 3rd parameter of lerp, which you’d be keeping track of and updating would tell you when the character has finished its move.

Oh, sorry, didn’t pay close enough attention to your code for a moment there. Your code doesn’t return the correct value right now (for moves). You’re trying to compare the variable of type ‘Move’ to a string.

One pretty simple way to use it could be to have an index on the playback script and use that to fetch the value from the recorder script. (You could also copy the array’s contents when you begin playback, and iterate over the copy, instead…)

1 Like