I have a 2D game and my goal is to make a car follow a path (the road in the picture) and rotate in the same time. I tried a plugin in the assets store but it didn't work well for me and i had difficulties making work as intended. If you have any suggestions, i ready to listen. Thanks for help in advance.
-
\$\begingroup\$ A way for us to reach out for the best solution to your problem would be for you to give the most detail possible of your implementation of the road (is it just an image, or a mesh, it is made on editor time with beizer curves or some sort of equation, etc.). A priori, I'd propose something like the Path-Following Steering Behavior (gamedevelopment.tutsplus.com/tutorials/…) given the lack of information, but if it were a mesh, nav-mesh could also work. \$\endgroup\$– LifGwaethrakindoCommented Jul 29, 2019 at 0:37
-
\$\begingroup\$ The main thing i want, is the Path-Following Steering Behavior like you said. And i'm looking for any solution to achieve this whether it is using a mesh or anything. \$\endgroup\$– Seif KarouiCommented Jul 29, 2019 at 1:25
-
\$\begingroup\$ I checked the link you provided and it seems written in Js which i'm not familiar with. \$\endgroup\$– Seif KarouiCommented Jul 29, 2019 at 1:37
-
\$\begingroup\$ Yes, it is. But the algorithm is in language-agnostic, one must always make an effort to interpret algorithms into the environment they are working on. A solution you may have for the first comment you made. An easy way would be for you to declare either a List or array of vectors as waypoint points, so each pair of vectors becomes a line segment. If you have a question regarding how to interpret certain Java stuff to Unity's C#, you can also feel free to ask, but do a previous effort for yourself. Good luck \$\endgroup\$– LifGwaethrakindoCommented Jul 29, 2019 at 3:35
-
\$\begingroup\$ When something "does not work", you can add details about the asset, the way you used it, the result you got and what you were expecting. Maybe the tool is right for you and you missed a way you could use it well for your needs. \$\endgroup\$– Vaillancourt ♦Commented Jul 30, 2019 at 17:43
2 Answers
Thanks everyone for your help. I wanted to say that i achieved the steering behavior of a car by applying what i found in this video(https://www.youtube.com/watch?v=11ofnLOE8pw&t=423s) I edited the scripts furthermore to suit my needs and i added this script which i found also in another video in order to make the car rotate along with the road above(https://www.youtube.com/watch?v=mKLp-2iseDc):
Vector3 targetInVec3 = new Vector3(targetPosition.x, targetPosition.y , 0f);
Vector3 direction2 = targetInVec3 - transform.position;
float angle = Mathf.Atan2(direction2.y, direction2.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = rotation;
Note: the angle variable can be changed to work with the initial rotation of your object. - target is the position of the next point on the curve.
If you have any question feel free to ask me.
Best way I can think of for it to follow the path is use way points, my only guess for the rotation would be having it look at the next rotation in the line.
-
1\$\begingroup\$ This answer would be better if it went into more detail about how to implement this suggestion. \$\endgroup\$– DMGregory ♦Commented Jul 30, 2019 at 12:03