You are on page 1of 1

First Unity Game – Waypoints Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoints : MonoBehaviour


{
public GameObject[] waypoints;
int current = 0;
float rotSpeed;
public float speed;
float WPradius = 1;

void Update()
{
//Establishes a pre-established forward/backward travel path for
objects/obstacles/enemies within the game world.
if(Vector2.Distance(waypoints[current].transform.position, transform.position) <
WPradius)
{
current++;
if(current >= waypoints.Length)
{
current = 0;
}
}
transform.position = Vector2.MoveTowards(transform.position,
waypoints[current].transform.position, Time.deltaTime * speed);
}
}

You might also like