TechManDylan

PlayerAlternate

Sep 18th, 2021
792
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerAlternate : MonoBehaviour
  6. {
  7.     public Route currentRoute;
  8.  
  9.     int routePosition;
  10.  
  11.     public int steps;
  12.  
  13.     bool isMoving;
  14.  
  15.  
  16.     private void Update()
  17.     {
  18.         if (Input.GetKeyDown(KeyCode.Space) && !isMoving)
  19.         {
  20.             steps = Random.Range(1, 7);
  21.             Debug.Log("Dice rolled " + steps);
  22.  
  23.             StartCoroutine(Move());
  24.  
  25.         }
  26.     }
  27.  
  28.     IEnumerator Move()
  29.     {
  30.         if (isMoving)
  31.         {
  32.             yield break;
  33.         }
  34.         isMoving = true;
  35.  
  36.         while (steps>0)
  37.         {
  38.  
  39.             routePosition++;
  40.             routePosition %= currentRoute.childNodeList.Count;
  41.  
  42.             Vector3 nextPos = currentRoute.childNodeList[routePosition].position;
  43.             while (MoveToNextNode(nextPos))
  44.             {
  45.                 yield return null;
  46.             }
  47.             yield return new WaitForSeconds(0.1f);
  48.             steps--;
  49.         }
  50.         isMoving = false;
  51.     }
  52.     bool MoveToNextNode(Vector3 goal)
  53.     {
  54.         return goal != (transform.position = Vector3.MoveTowards(transform.position, goal, 8f * Time.deltaTime));
  55.     }
  56. }
  57.  
RAW Paste Data