- Home /
 
Need help with switch case in order/timed
I'm making this game where you can move around and it will spawn a tail/path behind you, when i press spacebar it makes me go back to the start but it goes with a straight line, any way I can make it so it so it follows the path it took?
Like make the switch case execute the cases in order of how they got in ?
For example: I walk 3 times right, 2 up, 1 left and then 2 right again, so when i press spacebar i want it to go 2 times right, 1 left, 2 up and then 3 times right in that order.
Here's the code:
 public float speed = 2.0f;
     public float distance;
     public GameObject spawnprefab;
 
     private Vector3 pos;
     private Transform tr;
     private GameObject ins;
 
     List<int> steps = new List<int>();
 
     Player player;
 
     void Start () {
         pos = transform.position;
         tr = transform;
     }
 
     void Update () {
         
         if (Input.GetKey(KeyCode.D) && tr.position == pos) {
             pos += Vector3.right;
             tr.rotation = Quaternion.Euler(0, 0, 0);
             steps.Add (1);
             Spawnblock();
         }
         else if (Input.GetKey(KeyCode.A) && tr.position == pos) {
             pos += Vector3.left;
             tr.rotation = Quaternion.Euler(0, 0, -180);
             steps.Add (2);
             Spawnblock();
         }
         else if (Input.GetKey(KeyCode.W) && tr.position == pos) {
             pos += Vector3.up;
             tr.rotation = Quaternion.Euler(0, 0, 90);
             steps.Add (3);
             Spawnblock();
         }
         else if (Input.GetKey(KeyCode.S) && tr.position == pos) {
             pos += Vector3.down;
             tr.rotation = Quaternion.Euler(0, 0, -90);
             steps.Add (4);
             Spawnblock();
         }
         else if (Input.GetKeyDown(KeyCode.Space)) {
             CancelInvoke();
             ReturnToStart ();
         }
 
         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
     }
 
     void ReturnToStart()
     {
         for (int a = steps.Count - 1; a > 0; a--)
         {
             Debug.Log(steps[a]);
             switch (steps[a])
             {
                 case 1:
                     pos -= new Vector3(distance, 0, 0);
                     break;
                 case 2:
                     pos += new Vector3(distance, 0, 0);
                     break;
                 case 3:
                     pos -= new Vector3(0, distance, 0);
                     break;
                 case 4:
                     pos += new Vector3(0, distance, 0);
                     break;
             }
 
         }
     }
 
     void OnTriggerEnter(Collider col){
         if (Input.GetKey (KeyCode.Space)) {
             Destroy (col.gameObject);
         }
     }
 
     void Spawnblock(){
         ins = (GameObject)Instantiate (spawnprefab, pos-(transform.right), transform.rotation);
     }
 }
 
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
List.Sort with IComparer 2 Answers
Different timer in a list of gameobject 2 Answers
A node in a childnode? 1 Answer