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.
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.
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.