- Home /
[SOLVED, kinda]How to go about creating smoothly turning roads?
In 2D. I would like to make a rail(for a train game) sprite in which you can manipulate the end points to get a smoothly turning railroad. Just give me an idea on what's the best way to go about it. Thanks.
http://catlikecoding.com/unity/tutorials/curves-and-splines/
So I followed this tutorial got a bezier(smooth) curve which I could manipulate and add up more to it and then used two line renderers(which follow their spline) to draw the sides of my rails (which always stayed at the same distance, making it all work) and then the idea was to depending on the distace of the rail curve to create many new line renderers(in between my two main long curves) every n units but I didnt complete the second part of the idea cause it was too much effort for a small project.
I believe the best way to go is to just create railroad turn sprites/textures to each direction, there is not really a good way to edit 2D sprites/textures during runtime.
Answer by meltahawy · Jun 01, 2017 at 01:45 PM
Look into Unity spline solutions. I believe this asset might be of use to you: https://www.assetstore.unity3d.com/en/#!/content/7038 (Curvy - $50)
Here is another cheaper one to check out: https://www.assetstore.unity3d.com/en/#!/content/2506 (Simple Waypoint System - $15)
This tutorial is also very helpful if you would like to code a script on your own: http://catlikecoding.com/unity/tutorials/curves-and-splines/ (it is in 3D though)
Thanks for the answer. I actually found the catlike's tutorial recently and am following it through right now. I think it is the solution to my problem
Answer by GoldenProlix · May 25, 2017 at 07:07 AM
If you don't need it to be pin-point accurate (like just an animation) I would simply make a few different images in Photoshop and make an array containing with each image and then do something like this.
Update(){
timer += Time.DeltaTime
if(timer > animateFrameTime && isAnimating)
{
timer = 0;
showNextImageInArray();
if(imageShowing == imageToStopAt)
isAnimating = false;
}
}
This way you can choose the time it takes for each image to show and you can create in-between images so it looks like a smooth transition. : )
But how to go about making turns? This approach will work for straight rails but not really for smooth turns
I'm not 100% on the specifics of your game but say you were able to move the endpoints every 10 degrees. You would have to create an image for each of those 10 degrees (and if you wanted it to be extra smooth an image for every 5deg, 2deg...and so on) when the user is moving the endpoints you loop through the images to show the correct one. If you needed to have endpoints on both side of the rail road, all you would have to do is rotate the image until the start aligns and then offset your image array by that many degrees (if you rotate the image 90 degrees and your image interval was 5deg, then offset the array by 90/5 or 18).
Okay. Now I get what you mean. I will try this approach out. Thanks