- Home /
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
Long shot but did you find a solution to this by any chance as I seem to have the same problem @cgraf1
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.
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?
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.
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