Advertisement
Guest User

Untitled

a guest
May 13th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class FollowThePath : MonoBehaviour {
  4.  
  5.     public Transform[] waypoints;
  6.  
  7.     [SerializeField]
  8.     private float moveSpeed = 1f;
  9.  
  10.     [HideInInspector]
  11.     public int waypointIndex = 0;
  12.  
  13.     public bool moveAllowed = false;
  14.  
  15.     // Use this for initialization
  16.     private void Start () {
  17.         transform.position = waypoints[waypointIndex].transform.position;
  18.     }
  19.    
  20.     // Update is called once per frame
  21.     private void Update () {
  22.         if (moveAllowed)
  23.             Move();
  24.     }
  25.  
  26.     private void Move()
  27.     {
  28.         if (waypointIndex <= waypoints.Length - 1)
  29.         {
  30.             transform.position = Vector2.MoveTowards(transform.position,
  31.             waypoints[waypointIndex].transform.position,
  32.             moveSpeed * Time.deltaTime);
  33.  
  34.             if (transform.position == waypoints[waypointIndex].transform.position)
  35.             {
  36.                 waypointIndex += 1;
  37.             }
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement