using UnityEngine;
using System.Collections;
public class XY_MoveScript1 : MonoBehaviour
{
//Movement Parameters.
public Transform[] points;
public bool startAutomaticly;
public bool startFromTrigger;
public bool enableMultiPoints;
public GameObject triggerObject;
public Vector3 offset;
public float speed;
public float delayStartBy = 0;
private bool go;
private Transform destination;
private bool yes;
public bool switchIt = false;
int currentPoint = 0;
void Awake() { transform.position = points[0].position; }
void Start()
{
//---------------Through Errors if Needed----------------
if (!startFromTrigger && !startAutomaticly) { Debug.LogError("You have not specified to StartAutomaticly or StartFromTrigger!" + this.gameObject); return; }
if (startAutomaticly && startFromTrigger) { Debug.LogError("You have StartFromTrigger And Start automaticly checked on " + this.gameObject + "!"); return; }
if (startFromTrigger && !triggerObject) { Debug.LogError("You have Start from trigger checked, but you forgot to apply the TriggerGO1 prefab!" + this.gameObject); return; }
//-------------------------------------------------------
if (points.Length > 2) { enableMultiPoints = true; }
destination = points[currentPoint];
if (startAutomaticly)
{
StartCoroutine(WaitTime());
}
else if (startFromTrigger)
{
go= false;
GameObject trigger = Instantiate(triggerObject, transform.position + offset, transform.rotation) as GameObject;
}
}
void Update()
{
destination = points[currentPoint];
//So currentPoints isn't greater than points length. Or less than 0.
if (currentPoint >= points.Length) { currentPoint = points.Length; }
if(currentPoint <= 0) {currentPoint = 0;}
if (go && enableMultiPoints)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
if (currentPoint == points.Length) { switchIt = true; }
if (transform.position == destination.position && !switchIt)
{
currentPoint++;
}
else if (transform.position == destination.position && switchIt)
{
currentPoint--;
}
}
else if (go && !enableMultiPoints)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, destination.position, step);
if (transform.position == points[1].position)
{
destination = points[0];
}
else if (transform.position == points[0].position)
{
destination = points[1];
}
}
}
private IEnumerator WaitTime()
{
//Wait this long.
yield return new WaitForSeconds(delayStartBy);
go = true;
}
}