The code you've produced could only have two waypoints with reverse directions. I've prepared a simple kind-of-generic waypoint system which will help you produce your own.
Create a two C# scripts named WaypointInfo and WaypointSystem and copy/paste these:WaypointInfo.cs
using UnityEngine;
[System.Serializable]
public class WaypointInfo {
public Vector3 wayPoint;
// Use this for initialization
public bool IsWaypointReached(Vector3 movingObject, float deadZone = 0.3f)
{
if (Vector3.Distance(movingObject, wayPoint) < deadZone)
{
return true;
}
return false;
}
}WaypointSystem.cs
using UnityEngine;
public class WaypointSystem : MonoBehaviour {
public float moveSpeed = 5;
public float turnSpeed = 5;
public WaypointInfo[] wayPoints;
private WaypointInfo currentWayPoint;
private int currentWayPointIndex;
private Transform myTransform;
// Use this for initialization
void Awake ()
{
myTransform = transform; //assign the reference of Transform
if (wayPoints.Length > 0)
{
currentWayPoint = wayPoints[0];//set initial waypoint
currentWayPointIndex = 0;
}
else
{
Debug.LogError("No waypoint assigned");
}
}
// Update is called once per frame
void Update ()
{
//Turning the object to the target
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(currentWayPoint.wayPoint - myTransform.position), Time.deltaTime * turnSpeed); // Smooth turning
//Moving the object forwards
myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (currentWayPoint.IsWaypointReached(myTransform.position))
{
NextWaypoint();
}
}
///
/// Assign Next waypoint in the list.
///
private void NextWaypoint()
{
currentWayPointIndex++; // try to increase the index
if (currentWayPointIndex > wayPoints.Length - 1)
{
currentWayPointIndex = 0; // if index is larger than list of waypoints, reset it to zero
}
currentWayPoint = wayPoints[currentWayPointIndex]; // assign current waypoint from the list
}
private void OnDrawGizmosSelected()
{
for (int i = 0; i < wayPoints.Length; i++)
{
if (i == 0)
{
Gizmos.color = new Color(0, 0.4f, 0);
}
else
{
Gizmos.color = new Color(0.6f, 1, 0.6f);
}
Gizmos.DrawCube(wayPoints[i].wayPoint, new Vector3(0.3f, 1, 0.3f));
if (wayPoints.Length > 1)
{
Gizmos.color = Color.blue;
if (i == 0)
{
Gizmos.DrawLine(wayPoints[0].wayPoint, wayPoints[1].wayPoint);
}
else if (i == wayPoints.Length - 1)
{
Gizmos.DrawLine(wayPoints[i - 1].wayPoint, wayPoints[i].wayPoint);
Gizmos.color = Color.grey;
Gizmos.DrawLine(wayPoints[wayPoints.Length - 1].wayPoint, wayPoints[0].wayPoint);
}
else
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(wayPoints[i - 1].wayPoint, wayPoints[i].wayPoint);
}
}
}
}
}
Now:
- Assign WaypointSystem to a 3d game object.
- Set Move Speed and Turn Speed as you like(try 5 and 5 to see my results)
- Assign some Vector3 waypoints in the inspector(waypoints will be visible so you can place them as you want)
- Press Play, the object that you've attached the script to now should move between waypoints.
You can place as many waypoints as you want and script will take care of the rest, automatically loop between waypoints.
There is not much to explain, I tried to comment codes. The reason I made WaypointInfo a class instead of just Vector3 array is that in future you might want to add some new things in your waypoint system(perhaps you want to temporarily disable some waypoints in runtime, or something like that) you will be able to do it within the WaypointInfo class.
But here are some considerations( these stuff are probably advanced for you just now but you should consider these, for your future knowledge:
- Depending on your platform and your project size, if you are planning to use too much objects, this might become a problem because each object will have their own Update method and at some point performance will suffer(you need a lot of objects for this). You might want to change your implementation to have one update for all waypoint objects, call their own fake update methods from managing object. [Please refer here for more info.][1]
- Depending on your platform, Vector3.Distance used in WaypointInfo might be expensive. You might consider using Vector3.SqrMagnitude(in PC both methods produce same overhead, but in mobile, Vector3.Distance a lot more expensive than Vector3.SqrMagnitude). [Please refer here for more info.][2] Information provided in this link may or may not be changed since tests were made in 2012. You can test the difference yourself if you are really curious or need to squeeze every bit of performance.
Hope this helps you to understand better about waypoints and how to prepare a system that suits your needs. Feel free to ask anything that you didn't understand or curious about.
[1]: https://blogs.unity3d.com/2015/12/23/1k-update-calls/
[2]: http://answers.unity3d.com/questions/125882/vectordistance-performance.html
↧