Moving multiple objects along [bezier] path
What I'm trying to achieve is almost exactly the same as this -
A train that consists of dynamically added/removed carriages on runtime will move along bezier path which also "extends" whenever the train reaches the end of its current path by fired events.
New carriages will be added to the "head" of the moving train as it runs into them on its way along its path.
On each tap, the head carriage is thrown away with animation.
The above thread sounds like just what I need but then it was written in 2017 also it seems it only works with regular path not bezier path.
Is that still your recommended method? Also what about applying the same to bezier path?
Baroni last edited by
The script in the other thread uses
- tweenPath.PathGetPoint(perc) //for instantiating an object at a Vector3 position on the path
- objMove.tween.fullPosition //for setting the current movement position on the path, on the object's splineMove script
Both methods are DOTween methods, which have not changed. Have a look at its scripting reference:
A bezier path in SWS is different from a "regular path" in regards to its waypoint array. When spawning objects at waypoints, for a bezier path you obviously have to change the "waypoints" variable in that code, since it does not exist on a BezierPathManager. This would be bPoints instead.
The rest of it still looks like it would work.
Thanks for the explanation.
I've tried it now but it isn't working the way it's supposed to.
I tried to move 2 objects on the same path with some distance between them but they move together all along the path.
tweenPath.PathGetPoint(perc) seems to throw the exact same vector3 values for both objects even tho current distance and perc values are different. Not sure whether this is correct.
And objMove.tween.fullPosition are different for each object but both objects move at the same time anyway.
(btw I've replaced array with list because I plan on adding and removing objects on runtime)
GameObject GO = new GameObject("GN"); GO.AddComponent<splineMove>(); GO.AddComponent<PathSpawner>(); PathSpawner ps = GO.GetComponent<PathSpawner>(); //ps.onWaypointsOnly = true; ps.distanceMin = 0.02f; ps.distanceMax = 0.02f; ps.maxSpawnCount = 2; ps.objects.Add(player); ps.objects.Add(player); splineMove sp = GO.GetComponent<splineMove>(); sp.SetPath(WaypointManager.Paths[pm.name]);
Baroni last edited by Baroni
If you set the minimum and maximum distance to the same value, of course all objects will spawn at the same position.
The minimum and maximum distance describes the "length" of your path. A random position is chosen between both values for object placement.
Also, the PathSpawner sets the initial value of the object's path already. You cannot overwrite it via "SetPath" afterwards. The PathSpawner assign its own path to the spawned objects.
Ah sorry yeah I meant to post something else. That was one of my many tries. I've also tried different min/max values and no setpath either but the result was the same. Both objects move at the same time together.
GameObject GO = new GameObject("GN"); GO.AddComponent<splineMove>(); GO.AddComponent<PathSpawner>(); PathSpawner ps = GO.GetComponent<PathSpawner>(); //ps.onWaypointsOnly = true; ps.distanceMin = 2; ps.distanceMax = 4; ps.maxSpawnCount = 2; splineMove sp = GO.GetComponent<splineMove>(); sp.pathContainer = pm; ps.objects.Add(player); ps.objects.Add(player);
@Baroni Ugh how stupid! I figured it out. I've been setting the same game objects for the list instead of creating new objects for each. It all works now thank you :)
@Baroni One question tho.. So now I have multiple objects moving on the path separately, cool but then over time, the distance between each object becomes wider and wider meaning each object's speed changes over time. I need all the objects in the list to have the same speed unless I modify their speed at some point on runtime. Am I doing something wrong again..?
@Baroni Never mind.. I was wrong. Nothing has changed they still move at the same time from the same starting point all together and I can't figure out why.
@Hyunsik Figured out. Spline Move - On Start was checked.. so all objects started at the beginning of the path regardless.
So I just learned that this doesn't really work.
What you've suggested here /topic/2043/ is not "compatible" with DOTween's callback methods like OnComplete or Onkill etc because these methods will be called as soon as the FIRST object is done with the tween not the last. So you can't use these methods to detect completion of an entire tween for groups of objects.
I'm just gonna have to set individual tweens for every single object for the same path for example. multiple identical paths for multiple objects.