Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cgraf1 · Nov 09, 2018 at 04:23 AM · pathfindingwaypointstrack

Make object follow a track but player can move it side to side

Hello everyone, I want to make an object follow a specific track with turns, curves, etc. I also want this track to be fairly wide so the player can move this object side to side. The best way to describe it would be a race track with multiple cars. They are all following the same track, but can also move side to side, like on different lanes. Any and all help with this would be greatly appreciated. Thanks

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Roosiff · Sep 29, 2021 at 07:36 AM 0
Share

Long shot but did you find a solution to this by any chance as I seem to have the same problem @cgraf1

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by michi_b · Nov 09, 2018 at 06:41 AM

a common and relatively easy solution for a driving AI is to make it follow a precomputed "ideal" path. But the AI does not drive exactly on that path, but always drives directly to a point on that path that is always a certain amount ahead of it, therefore never reaching that point. The distance of that point should be far enough away from the car to avoid unnecessarily abrupt curves and the car can easily reach it considering its turn radius. But when it goes around a narrow curve, it must be close enough so the car does not get stuck in the curve, or you dynamically reduce the distance to the point on the path e.g. when an obstacle is encountered.

This way you can push the car away from the track and it will still know how to drive back. And you can implement detection of obstacles, evasion, and then resuming the behaviour of following the track. For multiple lanes, you could precompute multiple paths and make the driving AI consider switching it in case an obstacle is encountered, or even implement an evaluation of which lane is the shortest/fastest to drive on.

Of course, this kind of AI that does not use real pathfinding can get stuck between colliders and not find the way out, but traditional pathfinding does not give give results that consider the turn radius of a car.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cgraf1 · Nov 09, 2018 at 05:11 PM 0
Share

The "cars" I will be using will not have realistic turn radius or physics. I don't need something as complicated as detecting obstacles or the fastest lane. This is simply a case where normally I would have a constant Vector3.forward push and the player would being able to slide it left and right. With turns and curves, however, that would send them going off the track and I would like to make more interesting tracks. With your solution, could I have a point on the path that is just ahead of the player, but moves side to side with the player, so they are not always gravitating towards the center of the track? But then I would have to make multiple multiple paths for the point to follow with waypoints to handle how wide the track is. Does that sound right?

avatar image michi_b cgraf1 · Nov 09, 2018 at 09:33 PM 0
Share

That could work i guess. It really depends on how you want your gameplay to feel. If you do not want to have a fixed number of available tracks, you could also give the player a more dynamic control of where on the track they want to stay, e.g. with 0 being the left border and 1 the right border. Then you could interpolate between the curves for the left and the right border to get the actual curve you follow. The "follow a point ahead" method works independently of that, but is only useful if one or more of the following conditions apply:

  • the player can be pushed of the track or leave it by other means and should find his way back smoothly

  • you use linear segments ins$$anonymous$$d of curves to make the calculations more easy, but do not want to have "edgy" movement

  • you want to compensate variable turn radii of vehicles (which you said you don't)

If I understand your situation correctly, you want to use actual physics to move your "vehicles" or whatever. This makes them naturally leave their track when it is not perfectly straight. So I personally would build the track from quadrangular pieces and interpolate the current line the player follows inside these pieces based on his steering. Then I would always push the player towards a point ahead of him on the line he currently follows, or on the same interpolated line on the next segment he is about to enter. But that's just the solution for my current understanding of your situation, and also limited by my mathematical abilities. So it might not work for you but maybe it helps you figuring out your solution.

avatar image cgraf1 michi_b · Nov 12, 2018 at 12:00 AM 0
Share

What I went with was a constant Vector3.forward on the object and then at every turn there is a correctly angled trigger that changes the rotation to match the turn. Then the player can move the object left and right on the local X axis. The left and right movement works perfectly with the left and right arrow keys with the lines

 var move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
 transform.localPosition += move * move_speed * Time.deltaTime;

$$anonymous$$y issue now though is that I cannot get this left and right movement to work with a click and drag. Each time I try, by using On$$anonymous$$ouseDown, On$$anonymous$$ouseDrag, or something like

 var move = new Vector3(Input.mousePosition.x, 0, 0)
 transform.localPosition += move * move_speed * Time.deltaTime;

the object goes flying away like crazy. Any ideas? This may not be appropriate to continue on this thread, but I appreciate your help thus far $$anonymous$$ichi

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help understanding pathfinding 0 Answers

PathFinding with fixed waypoints problem 1 Answer

What is the best AI system for pathfinding on a moving platform 2 Answers

Waypoint tree structure 2 Answers

Move object along the path,Move an object along the fixed path 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges