Moving a shape automatically around a predefined path
Make sure that the object to be moved has a RigidBody applied to it.
Create a New C# Script
Rename the Script
Make sure that the script name and the class name in the file match.
If they don't the file won't compile
Attach the script to the object you want to move
Open the Script in the editor
Modify the class so that it matches the one below (changing classnames to match your own)
public class MoveEnemy : MonoBehaviour
{
public Transform[] target;
Rigidbody rb;
public float speed;
private int current;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
}
}
We add a RigidBody variable to store the RigidBody of the object
The Transform[] target is used later on to set waypoints for the object to move to.
public float speed is the speed we want to set the object to move at.
current stores the index of the current waypoint, the list of waypoints is stored in an array
Then in the Start() method we assign the objects RigidBody to the variable.
Be default we are given an Update() function. This will be called as often as possible by the computer.
This presents us with some problems.
If we are using a faster computer this will be called more often.
If we put the code into this function tell it to move 10 units every update a faster computer will move the object further in the same amount of time.
We can either use the FixedUpdate() method or ensure that any code we write in the Update() method is adjusted for the amount of time that has elapsed since the last update.
We cannot guarantee how often the Update() function will run. In this way it if similar to having a variable frame rate.
Here we can see that the FixedUpdate() provides regular consistent updates while the Update() can be inconsistent if not implemented carefully
Add the following code to the Update() function
// Update is called once per frame
void Update()
{
// It the object is not at the target / waypoint
if (transform.position != target[current].position)
{
// Set the position to move towards
Vector3 pos = Vector3.MoveTowards(transform.position,
target[current].position,
speed * Time.fixedDeltaTime);
// Update the position
rb.MovePosition(pos);
}
else // At the waypoint so get the next
{
current = (current + 1) % target.Length;
}
}
This code contains an if statement that will compare the objects current location with the waypoint we are moving to.
In the else section of the statement we are at the current waypoint so we get the next one.
We add one to the current index (current + 1) and then use modulo division % dividing it by the number of waypoints target.Length
This will get a 0 of the first index if we are at the last item and add one to it as if there are four waypoints the first is 0 and the last 3, 3 + 1 =4 and 4/4 has no remainder so it will reset to the 0 index.
Inside the true section of the if statement we need to get the position to move the object to.
Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.fixedDeltaTime); will create a three dimensional vector to move to.
MoveTowards is a function that takes three arguments.
- The current position of the object (transform.position)
- The position of the object to move towards / the destination target[current].position
- The speed to move the object. Note we use Time.fixedDeltaTime to ensure that we move the object by regular amounts when using the Update() function.
Next we need to create some waypoints to move towards
In Unity create an empty GameObject give this an appropriate name such as waypoint
Either make this a prefab and add duplicates or copy the object multiple times
Move the waypoints to the locations you want the object to move to.
Select the object you want to move, in this example it is enemy
In the inspector find the script and change the settings for the variables Target and Speed
Set the size of the list to the number of waypoints, you will then see that number of elements
Set the speed to the speed you want the object to move at.
Drag the waypoints into the correct element position in the list
You will now have an object moving around following a path