유니티 곡선 경로 생성기 V 1.0
아래에 한국어 번역이 있습니다. (There is an Korean translation at the bottom.)
Imagine that you make car running along curved road in Unity Scene.
You can make this car with Animator, but there are some problems.
Assum the object will move from red squre to blue squre.
There are so many ways to move object.
But If you implement this with Unity Animator, the Animator will choose the shortest path (orange line).
Suppose there is a path moving from a red square to a blue square through a green square as shown in the picture above.
When the distances of each point are not equal, if the animation keyframes are distributed as follows 1:1
The speed at which the object moves in the two sections is different.
So, if you want to make constant speed animation when s1 and s2 are not equal,
you should control keyframes to become S1 : S2 = ( t1 - t0 ) : ( t2 - t1 )
Of course, It's possible if you spend a lot of time.
But if the path is curved, It will be very hard to calculate the ratio.
The common issue of problems 1-1 and 1-2 is'curve'.
I was looking for how to express curves in Unity, and I found something called Bezier curves.
First, think of a point that moves a straight line.
There is a straight line and the point M is moving at a constant speed above it.
The trajectory of this point M is drawn as a simple straight line.
At this time, t is a number indicating how far the line segment has been proportionally advanced.
Add another line here and place a point on it that moves like an M.
Then, the original point M is referred to as M0, and the new point is referred to as M1.
The rules for moving M0 and M1 are the same as before.
Here you can draw one more line connecting M0 and M1.
The line naturally moves together when M0 and M1 move.
You can put the point on that line, and let that point be B.
And if you look at the trajectory drawn by point B, you can see that it becomes a curve drawn at a constant speed.
The trajectory drawn by point B is called a quadratic Bezier curve.
Below is the Bezier curve equation for time t.
As you increase the control point, you can create a 3rd, 4th, 5th order .. Bezier curve.
As the control point increases, you can create sophisticated or complex curves,
For this project, I thought that the quadratic Bezier curve was sufficient.
Unity Version : 2019.4.1f
There are 3 scripts I will introduce.
- PathGenerator : Script to make followable path the based on Bézier curve.
- PathGeneratorGUI : GUI Script to help generate path based on Bézier curve.
- PathFollower : Script to follow the path created by "Path Generator" class
So let's get started.
3-1-1 . Download latest release unity package or clone this repo.
3-1-2. Import Unity package.
3-2-1 . In your scene, create empty gameobject. (and rename to "Path".)
3-2-2 . This object will be followable curved path.
3-2-3 . Add "Path Generate" script on this object.
3-2-4 . Let's introduce parameters.
Put in a game object (prefab) to represent the "Flag".
This object doesn't matter what it looks like, but it shouldn't have a Collider.
(I recommend putting [Prefabs> Makers> Flag] to help you understand when you first start.)
The Flag is the point which is the objects must pass through.
(This is the same role as P0 and P2 in the picture above.)
Put in a game object (prefab) to represent the "Start Flag"
This object doesn't matter what it looks like, but it shouldn't have a Collider.
(I recommend putting [Prefabs> Makers> Start] to help you understand when you first start.)
Start Flag is the 0th Flag (FlagList[0]), which is the point where the object will start first.
Put in a game object (prefab) to represent the "Angle"
This object doesn't matter what it looks like, but it shouldn't have a Collider.
(I recommend putting [Prefabs> Makers> Angle] to help you understand when you first start.)
**Angle is placed between two flags. As the angle changes, the shape of the curve changes. **
This determines the path of movement.
(It is the same role as P1 in the picture above.)
Put in a game object (prefab) to represent the "Guide (Path)"
This object doesn't matter what it looks like, but it shouldn't have a Collider.
(I recommend putting [Prefabs> Makers> way] to help you understand when you first start.)
Guide visually expresses the path determined through Flags and Angles.
choose path type (Closed path or open path).
If this is true, auto connection between head of Flag List and tail of Flag List.
Determine to show Debug Objects (Flag,StarFlag,Angle).
Determine to show Debug Lines (Guide).
Decide how accurately you will draw the curve.
Higher numbers are closer to the ideal curve, but too high can cause objects to behave erratically.
Path Density should always be at least 2.
The recommended value is 30.
List of Flag. FlagList's Length should always be at least 2.
List of Angle. AngleList's Length should always be equal FlagList's Length.
3-2-5 . Fill in the blanks. In this tutorial, I'll use [Prefabs>Makers] elements.
3-2-6 . Choose the number of Flag List. If determine the number, the script automatically create objects.
3-2-7 . You can edit path to contol makers.
3-2-8 . Make your own path!
3-3-1 . Create object to move.
3-3-2 . Add "Path Follower" script.
3-3-3. This object should have a "Rigidbody" Component. (If it doesn't have it, automatically add.)
3-3-4 . Let's introduce parameters.
Input the function to run when it arrived final destination.
Remember! If the path is closed loop, there is no final destination.
Input the object with "Path Generator" component.
This object will move along this path.
Speed of movement. too high value can cause objects to behave erratically.
Speed of rotation. too high value can cause objects to behave erratically.
If the path is opened and this value is true,
the object teleport at Start Flag when it arrived final destination.
Remember! If the path is closed loop, there is no final destination.
If this value is false, object doesn't move.
3-3-5 . Fill the blanks.
3-3-6 . Object will move along the path.
There may be many examples, but the best example is to create an object that runs along a given track.
I once made a car that runs on a circular track I got from the Asset Store.
With a little modification, you can implement a car that moves naturally even the wheels and steering wheel.
You can express movements between planets like the solar system.
This script also allows you to create circular paths and elliptical orbits like Comet Halley.
Thanks for read! check out my blog too !
- Create issue in this repo
- [email protected]
- [email protected]
- [email protected]
한국어 번역은 추후 추가 예정
Korean translation will be added later