第一个原始代码是使用enum
和switch
,case
,state
的4个方向点,每个路点都有自己的功能。这工作正常。但现在我添加了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;
}
}
答案 0 :(得分:1)
这有一个很好实现的航路点系统,您可能需要查看:https://unity3d.com/learn/tutorials/topics/scripting/using-interfaces-make-state-machine-ai
你应该制作一个简单的公共GameObject []航点,并在那里拖放你想要的航点,并使用索引来循环它们。这可以替换第一行代码,因此您可以使用相同的方法。