sequence of commands

Hi there guys

I thought this must be easy, but I’m a bit stuck on hot to actually achieve this. I want my player to click on move buttons, then, when he is ready, click on “run” and the player(sphere) must follow these commands one after the other. For example, the user might click up, down, up and left. Then the player must move (walk) 1 unit (meter) up, 1 unit down, 1 up, 1 left etc. The tricky part is that the movement must be gradual and not instant.

Any ideas appreciated!

Thanks

You could create a List where each action the user performs adds a specific implementation of Command to the list, and then when you’re ready, execute the Commands in order. For example:

class Command
{
    public virtual IEnumerator Execute()
    {
        yield return null;
    }
}

Your base Command type would be something simple like that. Or you could create an interface instead. Whatever.

class MovePlayerCommand : Command
{

    private GameObject mPlayer;
    private Vector3 mTargetPosition;
    private float mSpeed;

    public MovePlayerCommand(GameObject player, Vector3 distance, float speed)
    {
        mPlayer = player;
        mTargetPosition = player.transform.position + distance;
        mSpeed= speed;
    }

    public override IEnumerator Execute()
    {
        while ((mTargetPosition - mPlayer.transform.position).sqrMagnitude > 0.01f)
        {
            float distanceToTarget = (mTargetPosition - mPlayer.transform.position).magnitude;
            float distanceToMove = Mathf.Min(mSpeed * Time.deltaTime, distanceToTarget);
            mPlayer.transform.position += (mTargetPosition - mPlayer.transform.position).normalized * distanceToMove;
            yield return null;
        }
        yield return null;
    }
}

And then you could execute the commands with something like:

List<Command> CmdList = new List<Command>();

public void AddCommand(Command cmd)
{
    CmdList.Add(cmd);
}

public void ExecuteCommandList()
{
    foreach(Command cmd in CmdList)
    {
        yield return StartCoroutine(cmd.Execute());
    }
}

Excuse errors in my code, and there are probably easier ways to create what you’re after, but hopefully this helps some!

Using MoveObject, you could do:

import System.Collections.Generic;
enum Dir {Left, Right, Forward, Back}
var speed = 1.0;

function Start () {
	var commands = new List.<Dir>();
	commands.AddRange([Dir.Left, Dir.Right, Dir.Forward, Dir.Forward, Dir.Back, Dir.Right]);
	FollowCommands (commands, transform, speed);
}

function FollowCommands (commands : List.<Dir>, thisTransform : Transform, thisSpeed : float) {
	for (var i = 0; i < commands.Count; i++) {
		var dir : Vector3;
		switch (commands[i]) {
			case Dir.Left: dir = -Vector3.right; break;
			case Dir.Right: dir = Vector3.right; break;
			case Dir.Forward: dir = Vector3.forward; break;
			case Dir.Back: dir = -Vector3.forward; break;			
		}
		yield MoveObject.use.Translation (thisTransform, dir, thisSpeed, MoveType.Speed);
	}
}

So you can populate a list of commands using whatever method, then pass that list to the FollowCommands function along with a Transform and the speed you want.

–Eric

Thanks for the replies

FM, let me chew on that a bit. I had my code very much like your bottom section, I will look at what you did in the first 2 code blocks