2d curved path movement project for Unity
This solution will allow you to build a path from bezier curves, then create a client and move it along the path.
⭐ The project has an OOP architecture, which makes it easy to integrate.
- Add an empty object to the scene. Add the "CurvedPath" script to it.
- Create a curve using this component.
- Create another object with the "ExamplePathClient" script. Specify in it the curve that you created before.
- ???
- Press play and have profit 😎
Make sure you have standalone Git installed first.
And paste this: https://github.com/YooPita/com.retrover.path2d.git
Or just copy the repository to your project files.
var points = new List<CurvePoint>();
You need to specify only the right handle. The left handle is a reflection of the right handle.
CurvePoint(Vector2 position, Vector2 rightHandle)
Path(List<CurvePoint> points, int precision, bool isLoop)
precision
- how many points will be set on the curve. The curve consists of segments in order to simplify the search.isLoop
- is the path closed.
Create a class and inherit from an interface IPathClient
.
In method UpdatePosition(PathPosition position)
, the client gets its position when it's updated. You need to use this position to transform your client.
PathPosition
has get-fields:
Vector2 Position
- position in 2d spaceNormalizedVector2 Normal
- forward directionfloat Length
- length from the beginning of the path
For convenient interaction with the path, use PathGrip
. Create an instance of this object and put it in a variable of type IPathGrip.
PathGrip(IPathClient client, IPath path)
- Give him yourself and the path on which you want to move.
- To attach your client to the path, use method
Attach(Vector2 position)
- Once a client has been attached to a path, it can be moved. To move the client forward by 1 pixel, pass
1f
to the followingMove(float movement)
method