Alright. So I’m trying to make a C# script that patrols a cube back and forth constantly. How would I do this efficiently? Keep in mind I’m going to have several different patrolling cubes/objects.
Could I set the cubes starting point and patrol to an empty game object? Or is that a bad idea? (Sorry I’m pretty new to coding and unity).
(Cube will patrol to X and back to it’s current position)
Yes, using empty GameObjects as your waypoints is a good plan - easy to place in the editor, got location data, etc. And GameObjects, in and of themselves, are pretty lightweight - you’re not going to impact your game’s performance by having a few hundred waypoints set. If you think it’ll be useful, you can create a minimal Waypoint script and attach it (remove its Update function), and you can store data there like how long it should wait at a given waypoint.
Be advised that these waypoints should NOT be children of the patrolling cube, or they’ll move with it.
Coding-wise, this is an excellent time to use coroutines.
public Transform[] waypoints;
public float speed = 1f;
void Start() {
StartCoroutine(Patrol() );
}
IEnumerator Patrol() {
while (true) {
for (int w=0;w<waypoints.Length;w++) {
Transform thisWaypoint = waypoints[w];
Transform nextWaypoint;
if (w+1 < waypoints.Length) nextWaypoint = waypoints[w+1];
else nextWaypoint = waypoints[0];
float moveTime = (nextWaypoint.position - waypoint.position).magnitude;
for (float t=0f; t<moveTime;t += Time.deltaTime) {
transform.position = Vector3.Lerp(thisWaypoint, nextWaypoint, t/moveTime);
yield return 0;
}
transform.position = nextWaypoint;
yield return new WaitForSeconds(1f); //pause when you reach the target
}
}
}
I stuck in a special little piece of code there - that second for loop is an unusual, but very handy little loop. It’ll run across a number of frames, and t/moveTime will gradually move from 0 to 1 across the course of those frames, which makes it handy to use as the last parameter of any number of Lerp functions.
You just attach that to the cube right? I’m getting a bunch of console errors and I’m not familiar with that code so I’m having a hard time figuring what’s going on.
Ah, yeah, I just typed that into the forums with no testing, so there are a few errors. On line 14 “waypoint” should be “thisWaypoint”, and on line 16 both parameters need “.position” after them.
You can add this function into the above class from @StarManta and see where the waypoints are in the editor scene window.
It uses the Unity editor gizmos. Once you start using these, you’re gonna LOVE them. You can even get a little crazier and make editable “handles” on each point. I’ll leave that part to you.
void OnDrawGizmos()
{
if (waypoints.Length > 0)
{
Vector3 offset = Vector3.up * 1.0f;
Vector3 last = Vector3.zero;
for (int i = 0; i < waypoints.Length; i++)
{
Vector3 curr = waypoints[i].position;
Gizmos.color = Color.yellow;
Gizmos.DrawLine( curr, curr + offset * 2);
if (i > 0)
{
Gizmos.color = Color.white;
Gizmos.DrawLine ( last + offset, curr + offset);
Gizmos.color = Color.black;
Gizmos.DrawLine ( last, curr);
}
last = curr;
}
}
}