Skip to content

Instantly share code, notes, and snippets.

@ghostofgamer
Last active October 1, 2023 06:32
Show Gist options
  • Save ghostofgamer/9c3c38f3bb7007505fc772e66e4c2bd4 to your computer and use it in GitHub Desktop.
Save ghostofgamer/9c3c38f3bb7007505fc772e66e4c2bd4 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private float _speed;
private int _index;
private Transform _allPlacesPoint;
private Transform[] _arrayPlaces;
private void Start()
{
_arrayPlaces = new Transform[_allPlacesPoint.childCount];
for (int i = 0; i < _allPlacesPoint.childCount; i++)
_arrayPlaces[i] = _allPlacesPoint.GetChild(i);
}
private void Update()
{
var pointInArray = _arrayPlaces[_index];
transform.position = Vector3.MoveTowards(transform.position, pointInArray.position, _speed * Time.deltaTime);
if (transform.position == pointInArray.position)
{
GetNextIndex();
GetNextPlace();
}
}
private void GetNextIndex()
{
_index++;
if (_index == _arrayPlaces.Length)
_index = 0;
}
private void GetNextPlace()
{
var pointVector = _arrayPlaces[_index].transform.position;
transform.forward = pointVector - transform.position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment