Advertisement
Dr_Max_Experience

Review 1

Aug 8th, 2022
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class WayPointMovement : MonoBehaviour
  6. {
  7.     [SerializeField] private Transform _path;
  8.     [SerializeField] private float _speed;
  9.  
  10.     private Transform[] _points;
  11.     private int _currentPoint;
  12.  
  13.     private void Start()
  14.     {
  15.         _points = new Transform[_path.childCount];
  16.  
  17.         for (int i = 0; i < _path.childCount; i++)
  18.         {
  19.             _points[i] = _path.GetChild(i);
  20.         }
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.         Transform target = _points[_currentPoint];
  26.  
  27.         transform.position = Vector3.MoveTowards(transform.position, target.position, _speed * Time.deltaTime);
  28.  
  29.         if (transform.position == target.position)
  30.         {
  31.             _currentPoint++;
  32.  
  33.             if (_currentPoint >= _points.Length)
  34.             {
  35.                 _currentPoint = 0;
  36.             }
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement