2017-02-18 22 views
-1

Первый оригинальный код был с 4-сторонними точками, используя enum и switch, case, state и каждый из них имеет свою функцию. Это работало нормально. Но теперь я добавил переменное имя wayPoints массив GameObject.Как я могу сделать массив точек точки вместо многих функций для каждой точки пути?

Вместо этого я хочу добавить новую функцию в каждую точку, чтобы сделать ее одной функцией. Например, допустим, у меня есть 30 кубов, и я добавляю их в массив wayPoints, и теперь я хочу, чтобы персонаж проходил между 30 кубами в качестве путевых точек. Поэтому я добавил новый State и назвал его WayPoints. Я также добавил новую функцию и назвал ее WayPoints()

Теперь я не уверен, как продолжить дальше. Я все еще хочу использовать идею enum с массивом wayPoints, но не уверен, правильно ли добавлен случай, и как это сделать в функции WayPoints().

using UnityEngine; 
using System.Collections; 

public class MoveObject : MonoBehaviour { 

    public GameObject[] wayPoints; 
    public Transform target; 
    float moveSpeed = 3f; 
    float rotationSpeed = 3f; 
    Transform myTransform; 
    State state; 
    public enum State 
    { 
     Idle, 
     Way1, 
     Way2, 
     Way3, 
     Way4, 
     WayPoints 
    } 
    void Awake() 
    { 
     myTransform = transform; 
    } 
    // Use this for initialization 
    void Start() 
    { 
     Debug.Log("Scripts Strart"); 
     state = State.Idle; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     Debug.Log("Update"); 
     switch (state) 
     { 
      case State.Idle: 
       Idle(); 
       break; 
      case State.WayPoints: 
       WayPoints(); 
       break; 
      case State.Way1: 
       waypoint1(); 
       break; 
      case State.Way2: 
       waypoint2(); 
       break; 
      case State.Way3: 
       waypoint3(); 
       break; 
      case State.Way4: 
       waypoint4(); 
       break; 
     } 
    } 
    public void Idle() 
    { 
     state = State.Way1; 
    } 

    void WayPoints() 
    { 
     for(int i = 0; i < wayPoints.Length; i++) 
     { 
      wayPoints[i].name = "wayPoint"; 
      target = GameObject.Find(wayPoints[i].name).transform; 
      float distance = Vector3.Distance(myTransform.position, target.transform.position); 
      Debug.DrawLine(target.transform.position, myTransform.position, Color.red); 
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); 

      //move towards the player 
      myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
      if (distance < 2f) 
      { 

      } 
     } 
    } 

    void waypoint1() 
    { 
     target = GameObject.Find("W1").transform; 
     float distance = Vector3.Distance(myTransform.position, target.transform.position); 
     Debug.DrawLine(target.transform.position, myTransform.position, Color.red); 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
     if (distance < 2f) 
      state = State.Way2; 
    } 
    void waypoint2() 
    { 
     target = GameObject.Find("W2").transform; 
     float distance = Vector3.Distance(myTransform.position, target.transform.position); 
     Debug.DrawLine(target.transform.position, myTransform.position, Color.red); 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
     if (distance < 2f) 
      state = State.Way3; 
    } 
    void waypoint3() 
    { 
     target = GameObject.Find("W3").transform; 
     float distance = Vector3.Distance(myTransform.position, target.transform.position); 
     Debug.DrawLine(target.transform.position, myTransform.position, Color.red); 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
     if (distance < 2f) 
      state = State.Way4; 
    } 
    void waypoint4() 
    { 
     target = GameObject.Find("W4").transform; 
     float distance = Vector3.Distance(myTransform.position, target.transform.position); 
     Debug.DrawLine(target.transform.position, myTransform.position, Color.red); 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
     if (distance < 2f) 
      state = State.Way1; 
    } 
} 

ответ

1

Это хорошо implented системы путевых точек вы можете захотеть взглянуть на: https://unity3d.com/learn/tutorials/topics/scripting/using-interfaces-make-state-machine-ai

Вы должны сделать простую общественность геймобжекты [] путевые точки и перетащить нужные точки маршрута туда и использовать индекс, чтобы зацикливать их. Это может заменить первую строку кода, чтобы вы могли использовать один и тот же метод.