0
\$\begingroup\$

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.enter image description here

\$\endgroup\$
  • \$\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\$ – LifGwaethrakindo Jul 29 '19 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 Karoui Jul 29 '19 at 1:25
  • \$\begingroup\$ I checked the link you provided and it seems written in Js which i'm not familiar with. \$\endgroup\$ – Seif Karoui Jul 29 '19 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\$ – LifGwaethrakindo Jul 29 '19 at 3:35
  • \$\begingroup\$ Thanks for your help \$\endgroup\$ – Seif Karoui Jul 29 '19 at 12:34
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ This answer would be better if it went into more detail about how to implement this suggestion. \$\endgroup\$ – DMGregory Jul 30 '19 at 12:03
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.