You can do something like the following, taken from the Space Shooter tutorial...
In your enemy's movement script FixedUpdate, the following code wouldn't allow them to move outside of your given boundaries on the X or Z axis. (Note, boundary is a reference to a Boundary in the game scene, you could substitute for float values if you know what you want them to be.)
GetComponent().position = new Vector3
(
Mathf.Clamp (GetComponent().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent().position.z, boundary.zMin, boundary.zMax)
);
↧