Retroever.Path2d
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.
How to start
- 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 😎
Installation
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.
Instruction
Path
1. Create a List of Curve Points
var points = new List<CurvePoint>();
2. Fill it with dots
You need to specify only the right handle. The left handle is a reflection of the right handle.
CurvePoint(Vector2 position, Vector2 rightHandle)
3. Create a path
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.
Client
1. Create a client
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
2. Communication with 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