Here's the route script if you're too lazy to copy it yourself: [SerializeField] private Transform[] controlPoints; private Vector2 gizmosPosition; private void OnDrawGizmos() { for(float t = 0; t
For people who are trying to make this in 3D Route script:- [SerializeField] private Transform[] controlPoints; private Vector3 gizmosPosition; private void OnDrawGizmos() { for (float t = 0; t routes.Length - 1) { routeToGo = 0; } coroutineAllowed = true; }
I know you shared this like 10 months ago, but by end the of the script you change the speedModifier by 0.90f: speedModifier = speedModifier * 0.90f; why exactly are you changing that value there? I don't see that line in the video and I was wondering why you added it. Thanks a ton for the script btw.
As others have said, this is one of the best Unity tutorials I've come across. The presentation of the information is concise, understandable and to-the-point. The tutorial moves quickly, but covers everything that one needs in order to achieve the goal of the lesson. There's no time wasted with turning on the camera to begin recording, describing how to make a unity project, stopping to answer disruptive DMs, trying to remember what the person was say, etc.--all of the problems with the majority of video tutorials out there today. Alexander's knows his stuff, knows what he wants to impart and does so, eloquently.
For anyone who wants the object to rotate along the bezier curve (like a train), simply add "transform.LookAt(position);" before line 51 in the bezierfollow script. Because the calculated position will be applied on the next line, this rotates your object towards that next point, then applies the transform.
Yes you are right, You can also do like this " Vector3 dir = objectPosition - transform.position; dir.y = 0; // if you want in sepecific directions only or its optional transform.rotation = Quaternion.LookRotation(dir); transform.position = objectPosition; "
MAN you are a coding god, i have been trying to learn bezier/spline curves for a while every tutorial is complicated as hell 33 Lines of code compared to my last bezier tutorial that was over 150 lines. thank you so much !!!
@@AlexanderZotov using the Unityeditor to make more complicated. How could i keep speed of the object the same on different routes with different sized curves i noticed on bigger curves the object i move is going faster than then on smaller ones any way to solve this? Thanks again
It was surprisingly difficult to find a tutorial to move an icon along a simple Bezier curve path...this is exactly what I was looking for, and as far as I can tell, it is one of a kind. Thank you!
One of the best Unity Tutorial Channels there is!!!! Should have a Billion Subs!!!!! Helped me many times this channel!!!!! No messing around and explains scripts in a good and clear way! Well done bro! keep up the good work!!!!!!!!!!!
If anyone doesn't want the movement to freeze up when you switch to scene, simply swap out WaitForEndOfFrame() with WaitForFIxedUpdate() - this will also make sure that the speed is independent of the frame rate.
this is amazing alex. your channel has once again saved the day. Every other tutorial on bezier curves is too complex, you simplified it! I used this to make 2d pendulum swing, and it could not be any better. you're the best,
Thank you so much for this tutorial! I'm really hesitant to use curves since I don't understand them yet but this was so easy to understand and modify to fit our game project! Now our player will bounce back smoothly from unwanted objects, just perfect!
Why to use Coroutine? If you are also wondering then this is what I understand.: First of all this is a very nice video explaining stuff even for beginners like me so thanks a lot to Alexander Zotov. I have subscribed to the channel as well. Then come to point. What I have understood (Please correct me fellows if I got it wrong) we need coroutine because other wise the while loop which is updating the position of the cat will run with in the single frame (and remember in 1 second there are 60 frames for 60 FPS rate) so the unity hangs and does not respond on such successive position updates with in a single frame. So we need to postpond this updation to the next frame so we are using "yield return" statement at end of each position updation at end of loop. However in my case yield return null worked because yield return new WaitForEndOfFrame caused unsteady updations instead of smooth.
Бро, спасибо тебе огромное, так долго пытался понять, что делать с моим простым перемещением, ибо не хотел переходить на движение с физикой и использованием AddForce. Про Bezier знал, но забыл, а на твоё видео наткнулся случайно, спасибо тебе большое, что напомнил!
You can also use transform.LookAt2D(p3, -90f); (The angle might differ based on the sprite) This will make the sprite look directly at the last waypoint. Really useful when you are spawning waves of enemies and want them to look at a specific direction with multiple routes.
Helpful, do you do this in the update()? i'm having trouble trouble trying to figure out how to have my sprite follow this curve, the sprite has 5 body parts and need them to move so looks kinda like a snake/serpent in space game, out of ideas
Thank you so much! I've been trying to find a way to do tank tracks in my 2D Tank Game. Now the next step is figuring out how to duplicate a single track enough times to make a perfect loop. Also have to figure out how I'm going to pull off the interaction of the tracks and wheels.
I notice the movement of the Cat on the Y is quicker than that of the X. I'm assuming that's because if you have a distance of 100, on the X that may equate to 10% of the overall screen width, where as 100 on the Y may equate to 30% of the overall screen height. Or to put it another way, the screen is wider than it is tall so the time it takes to travel the same distance is different from one axis to another. Does anyone have any suggestions on how to adjust the speed along the curves to account for this?
If you're having trouble seeing the spheres generated by the Route script, change the code on line 19 shown in the video to DrawWireSphere instead of DrawSphere.
if i wanted to do this, but for the camera, like the camera would follow that fixed line while following the player for example, the camera smoothly advances on that line as the player moves
I like this video, if you follow step by step its easy and fun, and I spend time make something with angles and its more hard than this and low universal. Thanks
Incredible, Alexander! I'm new to coding but I'm going to go ahead with this as a foundation and try to get my GameObject to move in this manner upon trigger without the repetition! Wish me luck. :D
This is a really great video and explains Bezier curves super clearly. I am wondering in terms of 3D space, is applying the Gizmos script just a matter of using Vector3 types rather than Vector2? How does the gizmoPosition formula change if using it in 3D space?
if you want here is a piece of code in order to make the cat rotate depending on where it's going the variable _angle is a private float by the way thank you a lot for the tutorial Vector3 relative = transform.InverseTransformPoint(_catPosition); _angle = Mathf.Atan2(relative.x, relative.y)*Mathf.Rad2Deg; transform.Rotate(0,0, -_angle); transform.position = _catPosition;
A small request which I am really in need. What if i want it to stop cat at one controlPoint for certain time and then move to next after some time. Appreciate your vidoes.
Hey! Awesome Tutorial. Can anyone tell me why we used coroutine and waitForEndOfFrame ? What would happpen otherwise? Why do we generally use this technique?
Hi, What I have understood (Please correct me other fellows if I got it wrong) we need coroutine because other wise the while loop which is updating the position of the cat will run with in the single frame (and remember in 1 second there are 60 frames for 60 FPS rate) so the unity hangs and does not respond on such successive position updates with in a single frame. So we need to postpond this updation to the next frame so we are using "yield return" statement at end of each position updation at end of loop. However in my case yield return null worked because yield return new WaitForEndOfFrame caused unsteady updations instead of smooth.
Hi Alexander, Thank you for this tutorial ! I have a little problem at 09:00. When my cat reaches the p0 of Route02, there is a 'jitter' due to the fact that 'route01 p4' and 'route02 p0' are the same point. When you run step by step in unity editor, you can see clearly that behaviour. Do you have a tip to avoid this between routes ?
Hi Alexander, Great tutorial thank you. I wanted to ask if this works with adding more than one game object to it. For example, if you wanted a train with multiple components on it. Would 'Hinge joint' work on it? Thank you
this is 2 weeks old but have you tried using Invoke? Invoke lets you perform a function after a set delay. Invoke("FunctionName", 5f) calls a function after 5 seconds
@@random_precision_software No problem Also if you want to keep spawning enemies for a set amount of time, you can use InvokeRepeating to save a few lines of code. Example: InvokeRepeating("SpawnEnemy", 2f, 5f) spawns an enemy after 2 seconds, then spawns anothet after 5 seconds again and again untill the function gets deactivated.
Depending on the curve, the transform your moving might move at different speeds dependant on what point of the curve their on. Do you know of any way to make the transform maintain a consistent speed?
The speed needs to be modified based on the distance traveled between the start and end points in a given curve. Speed becomes a multiplier, rather than a constant. The formula is then objectSpeed = speedMultiplier / distanceInCurve. It's a bit more complex, and outside of the scope of his tutorial, but that's the direction I've begun to pursue. Hopefully, that helps as a starting point... A simpler option is to make all of the curves approximately the same length.
Here's the route script if you're too lazy to copy it yourself:
[SerializeField]
private Transform[] controlPoints;
private Vector2 gizmosPosition;
private void OnDrawGizmos()
{
for(float t = 0; t
and here's the follow script:
[SerializeField]
private Transform[] routes;
private int routeToGo;
private float tParam;
private Vector2 objectPosition;
private float speedModifier;
private bool coroutineAllowed;
// Start is called before the first frame update
void Start()
{
routeToGo = 0;
tParam = 0f;
speedModifier = 0.5f;
coroutineAllowed = true;
}
// Update is called once per frame
void Update()
{
if (coroutineAllowed)
{
StartCoroutine(GoByTheRoute(routeToGo));
}
}
private IEnumerator GoByTheRoute(int routeNum)
{
coroutineAllowed = false;
Vector2 p0 = routes[routeNum].GetChild(0).position;
Vector2 p1 = routes[routeNum].GetChild(1).position;
Vector2 p2 = routes[routeNum].GetChild(2).position;
Vector2 p3 = routes[routeNum].GetChild(3).position;
while(tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
objectPosition = Mathf.Pow(1 - tParam, 3) * p0 + 3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 + 3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 + Mathf.Pow(tParam, 3) * p3;
transform.position = objectPosition;
yield return new WaitForEndOfFrame();
}
tParam = 0f;
routeToGo += 1;
if(routeToGo > routes.Length - 1)
{
routeToGo = 0;
}
coroutineAllowed = true;
}
@@niblet8308 you da real MVP
Thank you, god.
Thank you so much! 💎
My pleasure
For people who are trying to make this in 3D
Route script:-
[SerializeField]
private Transform[] controlPoints;
private Vector3 gizmosPosition;
private void OnDrawGizmos()
{
for (float t = 0; t routes.Length - 1)
{
routeToGo = 0;
}
coroutineAllowed = true;
}
life saver, ty
I know you shared this like 10 months ago, but by end the of the script you change the speedModifier by 0.90f:
speedModifier = speedModifier * 0.90f;
why exactly are you changing that value there? I don't see that line in the video and I was wondering why you added it.
Thanks a ton for the script btw.
Dude, yes.
@@danieldarko
That's not a necessary step. I just did it cuz I was testing something
sheeeesh, much thanks Abhinav:)
As others have said, this is one of the best Unity tutorials I've come across. The presentation of the information is concise, understandable and to-the-point. The tutorial moves quickly, but covers everything that one needs in order to achieve the goal of the lesson. There's no time wasted with turning on the camera to begin recording, describing how to make a unity project, stopping to answer disruptive DMs, trying to remember what the person was say, etc.--all of the problems with the majority of video tutorials out there today. Alexander's knows his stuff, knows what he wants to impart and does so, eloquently.
And this is one of the best comments I've read :-) Thank you very much!
For anyone who wants the object to rotate along the bezier curve (like a train), simply add "transform.LookAt(position);" before line 51 in the bezierfollow script. Because the calculated position will be applied on the next line, this rotates your object towards that next point, then applies the transform.
Muchas gracias..!!!, excelente..!!
Yes you are right, You can also do like this "
Vector3 dir = objectPosition - transform.position;
dir.y = 0; // if you want in sepecific directions only or its optional
transform.rotation = Quaternion.LookRotation(dir);
transform.position = objectPosition; "
que genio! muchas gracias
Worked like a charm for 3d movement, just changed "Vector2" for "Vector3"! Thank you so much!
Happy to help!
MAN you are a coding god, i have been trying to learn bezier/spline curves for a while every tutorial is complicated as hell 33 Lines of code compared to my last bezier tutorial that was over 150 lines. thank you so much !!!
Thanks for your feedback :-) Why 150 lines :-)
@@AlexanderZotov using the Unityeditor to make more complicated.
How could i keep speed of the object the same on different routes with different sized curves i noticed on bigger curves the object i move is going faster than then on smaller ones any way to solve this?
Thanks again
Speed depends on t. Gotta play with it to make speed to be constant.
@@AlexanderZotov yeah i believe i have done it now thanks
Cool!
I do not usually thank for tutorials but this was amazing
Thank you
Probably hands down the most useful tutorial I've seen in a really long time 😊
+1 like
Thank you!
Thanks For tuning in at Brackeys
It was surprisingly difficult to find a tutorial to move an icon along a simple Bezier curve path...this is exactly what I was looking for, and as far as I can tell, it is one of a kind. Thank you!
Happy to help. Thank you!
dude, this was amazing! I'm on a mission to implement some enemy/boss "swooping" motions at the player. This is the perfect foundation. Thank you!!
Happy to help
One of the best Unity Tutorial Channels there is!!!! Should have a Billion Subs!!!!! Helped me many times this channel!!!!! No messing around and explains scripts in a good and clear way! Well done bro! keep up the good work!!!!!!!!!!!
Thank you very much!
If anyone doesn't want the movement to freeze up when you switch to scene, simply swap out WaitForEndOfFrame() with WaitForFIxedUpdate() - this will also make sure that the speed is independent of the frame rate.
this is amazing alex. your channel has once again saved the day. Every other tutorial on bezier curves is too complex, you simplified it! I used this to make 2d pendulum swing, and it could not be any better. you're the best,
Thank you so much for this tutorial! I'm really hesitant to use curves since I don't understand them yet but this was so easy to understand and modify to fit our game project! Now our player will bounce back smoothly from unwanted objects, just perfect!
Happy to help :-)
Thank you very much! Wanted to do a shoot em up and give the enemies some smooth movement as they arrive onto screen and this is perfect for the job.
This tutorial is a best tutorial I have ever seen.
Thank you very much!
Why to use Coroutine? If you are also wondering then this is what I understand.: First of all this is a very nice video explaining stuff even for beginners like me so thanks a lot to Alexander Zotov. I have subscribed to the channel as well. Then come to point. What I have understood (Please correct me fellows if I got it wrong) we need coroutine because other wise the while loop which is updating the position of the cat will run with in the single frame (and remember in 1 second there are 60 frames for 60 FPS rate) so the unity hangs and does not respond on such successive position updates with in a single frame. So we need to postpond this updation to the next frame so we are using "yield return" statement at end of each position updation at end of loop. However in my case yield return null worked because yield return new WaitForEndOfFrame caused unsteady updations instead of smooth.
Thank you brother. I'm about to begin some Vehicle AI using bezier curves, this was an approach I was recommended.
Бро, спасибо тебе огромное, так долго пытался понять, что делать с моим простым перемещением, ибо не хотел переходить на движение с физикой и использованием AddForce. Про Bezier знал, но забыл, а на твоё видео наткнулся случайно, спасибо тебе большое, что напомнил!
Bezier Curves are Awesome. Thanks man for this Tutorial.
You are welcome!
You can also use
transform.LookAt2D(p3, -90f); (The angle might differ based on the sprite)
This will make the sprite look directly at the last waypoint. Really useful when you are spawning waves of enemies and want them to look at a specific direction with multiple routes.
:-)
Helpful, do you do this in the update()? i'm having trouble trouble trying to figure out how to have my sprite follow this curve, the sprite has 5 body parts and need them to move so looks kinda like a snake/serpent in space game, out of ideas
Man, this is AMAZING! Congratulations and thank you!
You are so smart.👍👌. This method can be used in various places such as enemy patrolling. Really helpful tutorial.
Thank you!
Thank you so much! I've been trying to find a way to do tank tracks in my 2D Tank Game. Now the next step is figuring out how to duplicate a single track enough times to make a perfect loop. Also have to figure out how I'm going to pull off the interaction of the tracks and wheels.
I notice the movement of the Cat on the Y is quicker than that of the X. I'm assuming that's because if you have a distance of 100, on the X that may equate to 10% of the overall screen width, where as 100 on the Y may equate to 30% of the overall screen height.
Or to put it another way, the screen is wider than it is tall so the time it takes to travel the same distance is different from one axis to another.
Does anyone have any suggestions on how to adjust the speed along the curves to account for this?
I found your channel today and I'm enjoying it so much!
You are welcome!
Thanks Alexander! Clear, concise and entertaining!
this is exactly what i was looking for! thank you for the tutorial! definitely gonna be messing around with it
Super! You are welcome!
Nice tutorial, Bezier is pretty tricky to learn, but you made it simple, thanks
Thanks for your feedback!
Great tutorial, how all tutorials should be clear and concise
lol " As we can see at this Wikipedia article a Bezier curve is a quite complicated shiiiiiii........ uh thing"
perfect!
FINALLY a guide worth following.
Thank you
cheers for the tutorial, I was able to use this for creating a rotating object with four platforms.
If you're having trouble seeing the spheres generated by the Route script, change the code on line 19 shown in the video to DrawWireSphere instead of DrawSphere.
I changed to drawwiresphere but still cannot sê the sphere
@@DuongNguyen-hx1lo increase radius of sphere, or enable gizmos tab in unity
All your tutorials are just amazing!. Keep going please!.
Excellent tutorial. Thanks Alexander!
if i wanted to do this, but for the camera, like the camera would follow that fixed line while following the player for example, the camera smoothly advances on that line as the player moves
Hi Alex, Brilliant tutorial.
Wow ! Thanks a lot ! You just saved my project right now...
Happy to help!
You made my day man! :) Kudos for the tutorial.
Thank you!
Thankyou Alexander great tutorial, you are one of the best in game tutoring finaly i can know add platforms to my game :)
You are welcome! Thanks for your feedback.
Very good stuff. I like to see that Im not the only crazy person that likes to use while loops XD
Yeah, "while"s can bring lots of headache :-)
I didn't know that you could do that with Unity. Great!
Thank you so much my guy! Looking all over for something like this
Happy to help
your tutorials are awesome u always give me exactly what i need thank u
Thank you, this tutorial is excellent!
You are a genius, thank you for this tutorial.
Happy to help
I like this video, if you follow step by step its easy and fun, and I spend time make something with angles and its more hard than this and low universal. Thanks
Thanks for your feedback!
this video is pure gold my hero
Thank you
Incredible, Alexander! I'm new to coding but I'm going to go ahead with this as a foundation and try to get my GameObject to move in this manner upon trigger without the repetition! Wish me luck. :D
Good luck :-)
@@AlexanderZotov Did it!
If anyone wants to know how, let me know.
@@CORKALOT I know it's been a year, but I'm stuck on how to stop gameobject when reaches the last point.
This is a really great video and explains Bezier curves super clearly. I am wondering in terms of 3D space, is applying the Gizmos script just a matter of using Vector3 types rather than Vector2? How does the gizmoPosition formula change if using it in 3D space?
Thanks! just what I want to implement in my game
You are welcome!
Hi, how do I fill up the transform array through script if I want to make the cat into a prefab?, since it would lose references to the routes...
Отличный урок,как и всегда!!
Спасибо!
Incredible stuff, thanks for sharing!
awesome work. exactly what I was looking for. thanks Alexander
You are welcome!
Thanks for the great how-to!
if you want here is a piece of code in order to make the cat rotate depending on where it's going
the variable _angle is a private float
by the way thank you a lot for the tutorial
Vector3 relative = transform.InverseTransformPoint(_catPosition);
_angle = Mathf.Atan2(relative.x, relative.y)*Mathf.Rad2Deg;
transform.Rotate(0,0, -_angle);
transform.position = _catPosition;
Man, i love you for that
Great! Exactly what I needed! Thanks.
My pleasure
0:28 Bezier Curve is a wide complicated Shi 🤣🤣🤣
I have benn looking for this video for a long time, since I did not search for bezier curve.
Your are welcome
nice tutorial!
Great video!! I am pretty new to Unity. This has really opened up my eyes to the potential of Unity. Thank you!
fantastically useful Alexander, thank you
A small request which I am really in need.
What if i want it to stop cat at one controlPoint for certain time and then move to next after some time.
Appreciate your vidoes.
Switch all the vector2's to vector3's if you want it to follow a curve in 3d space
thank you so much. It very well 💯
THIS WAS SO HELPFUL THANK YOU!
Hey! Awesome Tutorial.
Can anyone tell me why we used coroutine and waitForEndOfFrame ?
What would happpen otherwise?
Why do we generally use this technique?
Hi, What I have understood (Please correct me other fellows if I got it wrong) we need coroutine because other wise the while loop which is updating the position of the cat will run with in the single frame (and remember in 1 second there are 60 frames for 60 FPS rate) so the unity hangs and does not respond on such successive position updates with in a single frame. So we need to postpond this updation to the next frame so we are using "yield return" statement at end of each position updation at end of loop. However in my case yield return null worked because yield return new WaitForEndOfFrame caused unsteady updations instead of smooth.
Amanzing........congratulations!!!!
Transform.posion is ok but any suggestion for transform.rotation if it would be a car in place of cat
Amazing tutorial, thank you 🤩🤩🤩
You are welcome! Thanks for watching!
Thank you! Great tutorial.
You are welcome!
great tutorial, thank you very much
Hi, how can i implement dragging object through the bezier curve only. hope you help me thanks
This is really awesome. Thank you
My pleasure
Very interesting ! I love it.
👍
Awesome, thanks!
They way you cracked that shi*... joke.... so professional senpai! :D
Very nice tutorial, thanks :)
My pleasure!
Thank You ❤❤❤❤❤❤❤❤
Wow .. Awesome stuff .. you know your stuff .. Sorry for Redundancy XD
Thank you very much!
Hi Alexander, Thank you for this tutorial ! I have a little problem at 09:00. When my cat reaches the p0 of Route02, there is a 'jitter' due to the fact that 'route01 p4' and 'route02 p0' are the same point. When you run step by step in unity editor, you can see clearly that behaviour. Do you have a tip to avoid this between routes ?
Hi Sir, How can I correctly apply this to X, Z? So how can we apply it to the horizontal plane.
Can you help me? Thank you. Have A Good Day.
How can we make a line renderer on mouse position follow the path defined by bezian curve
VEry nice! Is it possible in an easy way to adjust the object rotation to aim always towards path?
hey how do you adjust how many gizmos are on the line of the curve?
Hi Alexander, Great tutorial thank you. I wanted to ask if this works with adding more than one game object to it. For example, if you wanted a train with multiple components on it. Would 'Hinge joint' work on it?
Thank you
speed is not same throughout animation ...How can we do that?
how to make it stop looping??
Great tutorial. But how can i show the curve in run-time in game view?
How would we place objects on Bezier surfaces with (u,v) versus t?
love this tutorial
Thanks!
U R ... GREAT!
Hey I was looking for this but in a different view, can we make a 2d laser with bezier curve?
Love it..I'm using it for a space enemy waves attack..but how do I have 10 enemies following the route a split second after each other?
this is 2 weeks old but have you tried using Invoke? Invoke lets you perform a function after a set delay.
Invoke("FunctionName", 5f)
calls a function after 5 seconds
@@TreacherousDev Yes, I could try that . THNX
@@random_precision_software No problem
Also if you want to keep spawning enemies for a set amount of time, you can use InvokeRepeating to save a few lines of code.
Example:
InvokeRepeating("SpawnEnemy", 2f, 5f)
spawns an enemy after 2 seconds, then spawns anothet after 5 seconds again and again untill the function gets deactivated.
is there anyway the object can go back the bezier curve while in the middle of it?
Depending on the curve, the transform your moving might move at different speeds dependant on what point of the curve their on. Do you know of any way to make the transform maintain a consistent speed?
Here is a comment in this thread right about it.
The speed needs to be modified based on the distance traveled between the start and end points in a given curve. Speed becomes a multiplier, rather than a constant. The formula is then objectSpeed = speedMultiplier / distanceInCurve. It's a bit more complex, and outside of the scope of his tutorial, but that's the direction I've begun to pursue. Hopefully, that helps as a starting point... A simpler option is to make all of the curves approximately the same length.
Want to give you a hug
Question: how do u find the midpoint, or is it as easy as adding the start point end point coordinate and dividing by 2?