Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple Waypoint System (SWS) - Move objects along paths

Discussion in 'Assets and Asset Store' started by Baroni, Dec 10, 2011.

  1. Hamad08

    Hamad08

    Joined:
    May 14, 2015
    Posts:
    9
    for example in fps scenario, creature moves towards a player recognizing the player transform position at runtime, without colliding with walls.....
     
  2. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    You wouldn't use a waypoint system for that. You can use the basic Unity Navigation System with obstacle avoidance.
     
  3. Hamad08

    Hamad08

    Joined:
    May 14, 2015
    Posts:
    9
    lol thanks for guiding...... i ll use it somewhere else :)
     
    Last edited: May 14, 2015
  4. scottCeci

    scottCeci

    Joined:
    May 15, 2015
    Posts:
    5
    It's my first week using Unity. Thank you for the waypoint system, it's a great help.

    What would be the easiest way to stop at a waypoint and look around until a d-pad button is pushed on the xbox controller? i can use the controller for other games but need the code to make the camera stop and look around before continuing on the path. I cant afford playmaker yet so any help is appreciated.

    Currently using the Nav Move Script, but cant figure out how to change the code to make it pause, but be able to look around. Thank you for your time.
     
  5. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    @scottCeci Thanks for using SWS!
    Easiest way to pause at a waypoint: waypoint events. Expand the events settings on your movement script and add a new event at the desired waypoint. Then drag in the movement script and select the Pause method.



    Keeping the seconds value at 0 will pause it forever, or until you resume it manually:
    Code (csharp):
    1. myObject.GetComponent<navMove>().Resume();
    How you implement the controller buttons and look around code is up to you, I'm sure you'll find some tutorials and sample code for that on the internet.
     
  6. scottCeci

    scottCeci

    Joined:
    May 15, 2015
    Posts:
    5
    Screen Shot 2015-05-15 at 9.00.55 AM.png

    this is the only option. There is no pause option.

    this code is for the button to resume correct?
     
  7. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Where it says "Monoscript.name", select the navMove script and Pause() method.

    Yes.
     
  8. scottCeci

    scottCeci

    Joined:
    May 15, 2015
    Posts:
    5
    @Baroni Thank you for actively helping. I really appreciate it.

    in the Monoscript.name the only option is string name.
    Screen Shot 2015-05-15 at 9.13.33 AM.png
     
  9. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    @scottCeci in this image you didn't specify the script on your object (below "Runtime Only"). In the last image you had the navMove script in there.
     
  10. scottCeci

    scottCeci

    Joined:
    May 15, 2015
    Posts:
    5
    The script is there. I deleted it and re-added it and same result. So i made a new capsule and tried splineMove. Same problem.
    Screen Shot 2015-05-15 at 9.31.49 AM.png
     
  11. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    @scottCeci do you drag in the splineMove script from the project panel by any chance? That would be incorrect, it has to be the object in your scene (Hierarchy) that has the script attached to it (meaning your capsule).
     
  12. scottCeci

    scottCeci

    Joined:
    May 15, 2015
    Posts:
    5
    @Baroni You're a genius. THANK You! I really appreciate the help.
     
    Baroni likes this.
  13. ProtoJazz

    ProtoJazz

    Joined:
    Nov 27, 2012
    Posts:
    19
    Ive got some crazy questions. Im working on a project where I have cars moving along a set path (Kind of like the kids toy electric racetrack) Where each piece of the track has its own specific path on it, and the cars should be able to follow that exact path, but also be able to vary in speed.

    So I guess what Im asking is , is this something I can achieve with SWS? Or should I be using something else?

    I see that I can place the paths in prefabs, so that would work, but Im not sure if theres any support for linking them together later. it doesn't need to be at run time even.

    Also it looks like the waypoints are just a list of transforms, would it be feasible to generate a path from a set of transforms? Without having to use the editor? Such as an exported spline from blender or other art programs would be in a list of vector3s, and a set of transforms could be generated at those points, then turned into a path.

    The other thing Im curious about is changing speed while moving along the path, is that posible?
     
  14. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Sure, I can't see reasons for that to be a problem.

    What do you mean by "linking them together"? Just instantiating the path and starting to move on it?

    You can create gameobjects as waypoints with an editor script and assign them to the waypoints array of a PathManager component, but you'll have to parse the data yourself obviously, depending on where you get them from.

    Yes. movementScript.ChangeSpeed(float value)
     
  15. ProtoJazz

    ProtoJazz

    Joined:
    Nov 27, 2012
    Posts:
    19
    Oh man, thanks. I was thinking this would be so much more dificult than it is.

    I figured id have to get the data and write the script myself. I just wanted to check before I did that it would even calculate the path without a lot of issue.

    I want to make tiles like these http://www.amazon.com/Scalextric-C8246-Track-Straight-inches/dp/B0007UBH2I/

    Where each tile has a path on it, so every time you lay down a tile it has exactly the same path.
    What Im wondering is if there is any way to turn all the seperate tile paths into a large path.
     
  16. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    The path is created at runtime based on the waypoints, no issue there.

    Hmm... yeah, an editor script could do that too. To combine two paths, you could do something like (pseudo code):
    Code (csharp):
    1. PathManager path1 = //get PathManager component from first selection in the editor (hierarchy)
    2. PathManager path2 = //get second path
    3.  
    4. //create combined array with the length of both paths
    5. Transform[] combined = new Transform[path1.waypoints.Length + path2.waypoints.Length];
    6. //then copy both path waypoint references over to the combined array
    7. ...
    8. //assign the combined array to the first path
    9. path1.waypoints = combined;
    10. //destroy the second path in the editor
    11. ...
    12. //done!
     
  17. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    I bought this asset and it's been great!

    It would be nice if there was option to check "Random starting point". Then it would randomly put the character (or whatever is in use) on one of the Waypoints available in the chosen Path.
     
  18. ahandyman

    ahandyman

    Joined:
    May 29, 2015
    Posts:
    2
    Hello

    Need help. Just downloaded the asset. All is great. Got the platform to move between the waypoints That was easy to build.

    My issue how to keep my First Person Character from not falling through the platform. I am using the FPS Controller from the Unity standard assets package.

    Tony
     
  19. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    @Nadan thanks!
    You can already do that now with a short script that sets the startPoint:
    Code (csharp):
    1. void Awake()
    2. {
    3.    splineMove move = GetComponent<splineMove>();
    4.    move.startPoint = UnityEngine.Random.Range(0, move.pathContainer.waypoints.Length);
    5. }
    Will consider adding that for the "random" loop type. Would you need it to be random when the object starts moving for the first time only, or every time StartMove() gets called (so basically after it stops and starts moving again too)?

    @ahandyman
    Hi Tony, does your platform have a collider and rigidbody (gravity disabled, kinematic enabled I suppose) attached to it? Otherwise just look what the other objects have attached to them where the FPS Controller is able to walk on.
     
  20. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    Only the first time. I have a city with people moving their paths. This adds variety to the game when they start from different places.

    Thanks for the script, it worked. I'm beginner with C#, so I'm not sure, but I added also this:

    Code (csharp):
    1. using SWS;
    Since it said: "The type or namespace name `splineMove' could not be found. Are you missing a using directive or an assembly reference?"

    I have a new challenge tough. If a character that is moving a path is killed, or run down by a car how do I stop the character from moving the path? I tried de-activating Spline Move -script (also tried to remove it in play mode) but the character keeps going. How could this be done? And would it be possible to assign the character to move back to the path. Say a car hits the character, character flys away from the car, then waits a while (with WaitForSeconds) and walks back to the path?
     
  21. Bralgs

    Bralgs

    Joined:
    Mar 1, 2013
    Posts:
    3
    Hi!
    Great Asset! But I need help.
    I use your Path Input Example for my game. But at the end of the path (when progress is equal to duration), the splineMove should change to the end of the next path (wich will be set on a trigger). The problem is, that the path duration value only will be updated on the next Update()-frame. I tried something like this:
    void Update()
    // ...........
    // ............ some code
    if(progress >= duration)
    {
    if(!CurrentPathContainer == null)
    {
    ChangePath ();
    //move.StartMove ();
    duration = move.tween.Duration();
    progress = duration - 0.001f;
    lookBack = true;
    }
    }
    public void ChangePath()
    {
    MoveSpeed = move.speed;
    move.ChangeSpeed (maxSpeed);
    move.SetPath (CurrentPathContainer); // CurrentPathContainer is set by trigger
    move.ChangeSpeed (MoveSpeed);
    //move.StartMove();
    //move.speed = 0.01f;
    //move.Pause();
    }
    When I debug my code, I see that the move.pathContainer will be updated in the code, but in the scene view (in Unity) the new path will be hilighted only on the next Update()-frame. Do you know, how I can get the new duration value before the next Update()-frame starts? Sorry for my bad english ;D
     
  22. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    movementScript.Stop()

    That would require you to use pathfinding, as your character has to "find" its way back to the path. That should work with the navMove script and Unity's NavMesh system. In the event of a collision with the car, call Pause() on the navMove script and let your object fly off. When it should walk back to its path, call Resume().

    Hi Bralgs, the movement script needs one frame to calculate the path, that's why there is one frame of delay. The easiest way would actually be to use a coroutine and wait for that frame, before setting the duration/progress. You can also try to set the duration value to something very high instead (e.g. 1000), as DOTween will clamp it to the actual end value if it is higher than that.
     
    Bralgs likes this.
  23. vra_gou

    vra_gou

    Joined:
    Jul 11, 2013
    Posts:
    3
    Hi there Baroni,

    I have a problem accessing the path waypoint component in the splineMove script. I'm trying to access it from an enemy gameobject that is instantiated from a spawn point. That spawn point holds two spline paths that the enemy will choose from the player position. But: currentEnemy.gameobject.getComponent<splineMove>().pathContainer just won't be accessed. What am I missing? Everything's set to public? My goal is for an instantiated enemy to pick up a spline path at their creation that is the closest to the player, and went it will get close enough of the player, snap out of the spline path and chase the player with a raycast system that works pretty well.

    Thanks.
     
  24. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Hi, what is the exact error message?
     
  25. vra_gou

    vra_gou

    Joined:
    Jul 11, 2013
    Posts:
    3
    Hey thanks for the quick reply. I got what I wanted searching a bit more on your forum!

    here's what I was looking for really:

    currentEnemy.gameObject.GetComponent<SWS.splineMove>().SetPath()
     
  26. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Yup that's what I thought of too. Or just put "using SWS;" at the top of your script, so you don't have to include the namespace every time.
     
  27. vra_gou

    vra_gou

    Joined:
    Jul 11, 2013
    Posts:
    3
    Ok I'm pretty much stuck here. I've tryed alot of combination to get the .SetPath() to work, and I can't figure it out. Guess that show the level of my scripting skills... lol.

    How the SetPath() works exactly? I used arrays to store the waypoints I need: waypoints[]

    And I need to put that into the .SetPath, but I can't figure how.

    i.e.:

    currentEnemy.gameObject.GetComponent<splineMove>().SetPath(localWaypoints[1]);
    currentEnemy.gameObject.GetComponent<splineMove>().SetPath(PathManager = localWaypoints[1]);
    currentEnemy.gameObject.GetComponent<splineMove>().SetPath(PathManager (localWaypoints[1]));
    currentEnemy.gameObject.GetComponent<splineMove>().SetPath(PathManager = new Transform (localWaypoints[1]));

    Edit: I have the using SWS on top of my script.
     
    Last edited: Jun 9, 2015
  28. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    You don't want to pass individual waypoints into SetPath(). From the documentation: SetPath(PathManager newPath) Changes the current path of this object and starts movement.

    It expects a path as argument, not gameobjects. There is a sample in the runtime scene on how to call it via code. You can pass in a PathManager component directly if you have a reference, or use the name like this: SetPath(WaypointManager.Paths["myPath"]).

    If you want to create a new path of your waypoints at runtime, you first have to create a gameobject and add the PathManager component to it. Next access its waypoints array to set the waypoint transform. Then you are able to pass in the PathManager component into SetPath() of a movement script.
     
    vra_gou likes this.
  29. alexbrandt

    alexbrandt

    Joined:
    May 11, 2015
    Posts:
    2
    Hi Baroni,

    I'm currently using SWS for a traffic system where vehicles change to the beginning of another path when they reach the end of their current path. My issue is that a few paths, not all, create a path the extends before the first waypoint so the vehicles end up moving backwards for half a second and then forward again. On initialization I also have the cars spawn at the second waypoint, index 1. However on the paths which have this issue of extending backward the car will spawn at the first waypoint as if it was the second. I attached some pictures showing the same path, with and without the issue. I was able to fix the issue by just moving the first waypoint around a little until it works. But then trying to reproduce the issue it seems that the conditions that cause this extrapolating is completely arbitrary. Have you seen this issue before or know the cause of this behaviour? Thanks!

    Capture.PNG

    Capture2.PNG
     
  30. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Hi alex,
    are you using the latest version of SWS (5.0.3)? This seems to be an issue with DOTween, as SWS does not calculate paths itself. Could you try to place both waypoints at the exact same position and see if it gets rid of the error? Also I would like to have a simple repro for this, if you could send it to my email (stated on the last page in the documentation pdf).
     
  31. VR2Tuff

    VR2Tuff

    Joined:
    Nov 16, 2014
    Posts:
    7
    This asset seems amazing (just purchased) though have not dug in deep....I wanted to report the first thing I found though....

    Using 5.1, the waypoint manager does not seem to work. I cannot enter in a path name therefore cannot 'start a path'. I have not yet tried downgrading to 5.0.1 as I'm not quite sure if I'm just missing something.

    Thanks!
     
  32. yuhe

    yuhe

    Joined:
    Dec 20, 2012
    Posts:
    11
    I changed all references to SceneView.currentDrawingSceneView to SceneView.lastActiveSceneView in WaypointEditor.cs, and it seems to have fixed it.
     
    Baroni and VR2Tuff like this.
  33. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    I haven't had the time to look at it yet, thanks for reporting and pointing that out!
     
  34. alexbrandt

    alexbrandt

    Joined:
    May 11, 2015
    Posts:
    2
    I seem to have figured it out. I had all the vehicles as a child of an empty game object which was not positioned at (0,0,0). Having changed that parent object to be at (0,0,0) it all works fine now. DOTween must have been doing some weird math between local positions and world positions.. Thanks for the help though!
     
    Baroni likes this.
  35. VR2Tuff

    VR2Tuff

    Joined:
    Nov 16, 2014
    Posts:
    7
    Thank you! Going to test ASAP :)
     
  36. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    Great asset!

    However, I have an event occurring at a waypoint node (AudioSource.pause). The audio source does pause once the waypoint node is reached, but the audio source does not continue upon exiting the waypoint node. Any help is appreciated (NOTE: I am approaching a deadline, so the sooner the better. Thanks again!)
     
  37. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    What code are you using for that event, to pause and resume it?
     
  38. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    Thanks for your support. You can see in the attached image (see Screenshot 359) I have a 'spline move' script attached to a gameobject (in this case, a deer). You can see in Screenshot 360, in the 'Show Events' section, the gameobject is being told to pause for 5sec and pause audio source, but there is no float value for the audio pause.

    The gameobject's movement does pause at the waypoint and continue after exiting. The audio also pauses at the waypoint, but does not continue after exiting.
     

    Attached Files:

  39. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Thanks for the screenshots. Yes, you are telling the AudioSource to pause at the waypoint, but not to resume. The movement script does not know any connections to other components either, so it can't resume your AudioSource automatically. What you need is a custom script that does both of this.

    Code (csharp):
    1. //new method as UnityEvents can't start coroutines
    2. public void PauseAll(float seconds)
    3. {
    4.    StartCoroutine(Pause(seconds));
    5. }
    6.  
    7. //the actual pause method
    8. IEnumerator Pause(float seconds)
    9. {
    10.    //pause movement sript
    11.    GetComponent<SIS.splineMove>().Pause(seconds);
    12.  
    13.    //pause audiosource
    14.    AudioSource audioSource = GetComponent<AudioSource>();
    15.    audioSource.Pause();
    16.    yield return new WaitForSeconds(seconds);
    17.    audioSource.UnPause();
    18. }
    Create a new C# script, copy-paste the above code and attach it to your object. Then call the PauseAll() method on it via the event settings.
     
  40. AdeelAbbas

    AdeelAbbas

    Joined:
    Jul 18, 2014
    Posts:
    22
    Hi Baroni can you please tell me Why My character falls after completion of path. Whenever I enable my moving script it works fine but at the end of path my character falls.
     
  41. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    A rigidbody component with gravity enabled on your character?
     
  42. AdeelAbbas

    AdeelAbbas

    Joined:
    Jul 18, 2014
    Posts:
    22
    No I disable gravity from Character. On My Character I use Character Controller for moving Character but my moving Character logic start after completion of my path.
     
  43. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Character Controllers have gravity built-in...
     
  44. buttmatrix

    buttmatrix

    Joined:
    Mar 23, 2015
    Posts:
    609
    First, thank you so much for your help in this matter, and if I haven't given you 5 stars already, I certainly will now. The script did not compile at first, but I hacked around with it a bit, and it eventually worked. Please see attached for the final version. Thanks again!
     

    Attached Files:

    Baroni likes this.
  45. CronoCaracas

    CronoCaracas

    Joined:
    Apr 23, 2015
    Posts:
    3
    Greetings baroni, I highly appreciate what you are doing here. I have some difficulty, I am working on a project where I want to use real positional data based on coordinate systems in relation to the center of the world (ie: -41, 39, etc) in a second to second basis, I also have the speed of the objects but basically is a second to second movement simulation, do you have any particular suggestion to approach this? I would highly appreciate any feedback, specially since I am quite new to unity. Beforehand, thank you and keep up your awesome work!
     
  46. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Hi Crono,
    does second to second basis mean you get new world positions every second and want to move your object on that? In that case this would require you to rebuild the path every second and start on the last waypoint, as tween paths can't update. Adding waypoints to a path is no problem, but that is definitely a hurdle. If I didn't get it, could you describe your use case a little bit more?
     
  47. CronoCaracas

    CronoCaracas

    Joined:
    Apr 23, 2015
    Posts:
    3
    Hi Baroni,
    Thank you so much for your prompt response. I am working on a simulation where I get the real movement data, specifically I have access to direction, position and speed (in relation to the center of the plane), this info is given pre-loaded in a second to second basis in a CSV file, so it is more like reproducing the real movement already registered and scripted than needing some real time refreshing rate. My final objective is reproducing this movement as close as possible to reality for observational and visualization purposes.

    Again, thank you so much for the invaluable feedback and sorry for my limited knowledge of the platform, I plan on getting my hands dirty non stop from now on.
     
  48. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    @CronoCaracas Ah, thanks for the explanation!
    The first thing you would do is to convert your CSV data into Unity arrays, so you have array variables for position, speed and rotation. Code snippets about that could be out there somewhere.
    • Then you can set the waypoints of a PathManager in SWS by implementing this suggestion. At this point, you should have the position movement covered.
    • Adjusting the speed could be done by calling yourMovementScript.ChangeSpeed(float value) with the current value in your speed array, for example every second in a while-Coroutine.
    • For the rotation, you could use the orientation setting built-in (orientates your object to the path), or write your own method that lerps to the new rotation value every second.
    There is much to look up for a user new to Unity and I've tried to reference most of the methods with their scripting part :)
     
    CronoCaracas likes this.
  49. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160
    Hello @Baroni

    Sorry is this has already been asked. After reading all post for half an hour i gave up.

    I am at a step where i make the behavior of my last unit in my game, which is a "Swarmer" ...
    Kind of like a flying bomb , that follow its path to the main "level target" ..
    Along the path, i make it detect enemy units. if it finds one, i make a "splineMove.pause (0.0f)", and take over its direction and speed.
    The issue i have is : Sometimes the target gets destroyed before the swarmer reaches it.
    So i tried a few things, trying call "GoToWaypoint" or "Resume" or "Pause (0.01f)" on the spline move. BUt it does not go back to its waypoint smoothly, it gets teleported to the waypoint.
    So before i make a function that make it go back to the "currentPoint" smoothly, detect distance to it and if close enough, "Resume" the tween .. is there an easier, better way ? if i could avoid that function, that would be great... my swarmers comes in very high numbers ( between 200 and 300 ). Because of SWS, it all goes very smoothly on a low end laptop computer, even with all those units on the screen, and i would like to keep it that way.
    May be there is way to "Re-create" the path from its "Transform.position" as index[0] and add the currentWaypoint+n ? something like that ?

    Thanks in advance, and keep it up ! your system makes my sad laptop showing 500 units moving smoothly. i was lock at 150 when using an A* pointGraph system ( worth mentionning that all those units are detecting enemys, aiming, exploding and all that at...)
     
    Last edited: Jun 17, 2015
  50. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    Yes! Optionally, replace splineMove.Pause() with Stop(). Then do this instead of calling Resume():
    Code (csharp):
    1. splineMove.moveToPath = true;
    2. splineMove.StartMove();
    This will move to the "currentPoint" waypoint and start moving from there. Glad you're satisfied with the performance!