using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class WayPoints : MonoBehaviour {
public bool stateForward = false, stateReverse = false, stateRandom = false;
public GameObject[] waypoints;
public Transform waypoint;
public bool random = false;
public float moveSpeed = 5f;
public float slowDownSpeed = 1.5f;
public float rotationSpeed = 1f;
private int targetIndex = 0;
private Vector3 originalPosition;
private GameObject[] objectsToMove;
private Animations anims;
void Awake()
{
}
// Use this for initialization
void Start()
{
anims = GetComponent<Animations>();
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
objectsToMove = GameObject.FindGameObjectsWithTag("Robots");
originalPosition = objectsToMove[0].transform.position;
}
// Update is called once per frame
void Update()
{
if (MyCommands.walkbetweenwaypoints == true)
{
//DrawLinesInScene();
anims.PlayState(Animations.AnimatorStates.RUN);
WayPointsAI();
}
}
private void WayPointsAI()
{
if (stateForward == true)
{
stateReverse = false;
stateRandom = false;
if (targetIndex == waypoints.Length)
targetIndex = 0;
waypoint = waypoints[targetIndex].transform;
float distance = Vector3.Distance(objectsToMove[0].transform.position, waypoint.transform.position);
objectsToMove[0].transform.rotation = Quaternion.Slerp(objectsToMove[0].transform.rotation, Quaternion.LookRotation(waypoint.position - objectsToMove[0].transform.position), rotationSpeed * Time.deltaTime);
//move towards the player
if (distance < 30)
{
objectsToMove[0].transform.position += objectsToMove[0].transform.forward * slowDownSpeed * Time.deltaTime;
}
else
{
objectsToMove[0].transform.position += objectsToMove[0].transform.forward * moveSpeed * Time.deltaTime;
}
if (distance < 2)
{
targetIndex++;
}
}
}
void AddColliderToWaypoints()
{
foreach (GameObject go in waypoints)
{
SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
sc.isTrigger = true;
}
}
}
I want that in the function WayPointsAI if the checkbox stateForward the objectsToMove will move between the waypoints from the first waypoint in the List waypoints to the last waypoint.
If the checkbox stateReverse is checked then move the objectsToMove between the waypoints but this time from the last waypoint in the List to the first waypoint.
If the checkbox stateRandom is checked make the objectsToMove to move between the waypoints random.
How can i do it by code and how can i do it in one function in the WayPointsAI ? Or maybe i should add two more functions each function for each state ?