How would you go about creating a path that a gameobject would follow?

I'm looking to create similar functionality that you see in some RTS games. most notability the Total War series does this well. here are a few screen shots for reference.



  1. Click a Unit
  2. Hold the mouse button down and drag to draw a line
  3. Release the mouse button. unit follows path drawn

If you are talking about a simple (straight) line, you can simply create two Vector3, one called startPosition which equals to the point that you clicked, and one called endPosition which while holding your mouse it equals the mouse location, and then you release it, it keeps that last value. After that, how to draw the path is up to you. You can use a LineRenderer with the above coordinates, or you can be more creative with a 3D mesh.

If you want to make something more complicated, like a line that has angles, you can still use LineRenderer, just create multiple points, and each time you click the mouse (or any other input you like) create a new point on that position.

If you want curves, you can keep all the points like above, but you can also add control points (depending what kind of curve you want to make) and calculate their position based on their surrounding points, to make a smooth curve. If interested I'd suggest taking a look at Bezier Curves (Wikipedia). After you have the points (and their control points), there are some options how to draw them on the screen. I'd suggest taking a look at this tutorial.

|improve this answer|||||
  • \$\begingroup\$ If I'm able to create a Bezier curve I should be able to have the game object move to each point in the list and follow the path. \$\endgroup\$ – Pyreal May 3 '18 at 14:58
  • \$\begingroup\$ @Pyreal Exactly. Using Bezier curve you can create a 3D object along the curve, to create a line of 3D objects and also use the LookAt to make them smooth. \$\endgroup\$ – TomTsagk May 3 '18 at 15:38
  • \$\begingroup\$ Thanks, I'll post my results once I get it implemented so others can use it. \$\endgroup\$ – Pyreal May 3 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.