Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview 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. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Can't find the link on my mobile phone so I'm reposting instructions from a recent email conversation.

    To create a path at runtime:

    - add a PathManager component to a gameobject
    - add the path component to the WaypointManager paths dictionary
    - create multiple gameobjects acting as waypoints and position them in your scene
    - create an array with these waypoints and assign it to your PathManager’s waypoints array
    - done.
     
  2. hottabych

    hottabych

    Joined:
    Apr 18, 2015
    Posts:
    107
    Question
    How to shift my object from the spline to the left or to the right?
    Imagine the spaceship flying along the path. I want to control it with left/right arrows to move it across the racetrack.
     
  3. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Simply make your object the child of another gameobject with a movement script, then modify the child position locally.
     
    hottabych likes this.
  4. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Hello,

    i am using Core Gamekit for spawning my prefab enemies from a pool in 2D environment. How can i apply a waypoint to the prefab so it can start moving on a waypoint as soon as it is spawned?
     
  5. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @Kruko,

    please see the runtime example scene for samples on what do use at runtime. You can call SetPath on the movement script with the path after your object has been spawned. Also make sure to uncheck "On Start" on the movement script prefab.
     
  6. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Thank you, i'll give it a try.
     
  7. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    SetPath works :)

    One more question, i can't seem to find it in the documentation. My sprites are rotating while following the curve, how can i lock them to stay as they are while moving through the curve? Lock rotation doesn't seem to do it.
     
  8. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @Kruko for that you would simply set path mode (3d, 2d sidescroller, 2d top down) to none.
     
  9. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Works like a charm, thank you!
     
    Baroni likes this.
  10. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    Forgive me if this is a repeat the forum post are getting lengthy

    i have a character using navMove and when it collides with a enemy i want to have it goto waypoint 0
    basicly i would like to call at any point in the game to change its waypoint destination.
    all methods ive tryed teleport it, i rather it not do that . i prefer it to set a new destination could you post an example or point me to an example code.
     
  11. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Thank you for your feedback @wethecom, I will look into making the API more consistent regarding teleporting. In the latest version you can use:

    Code (csharp):
    1. yourNavMove.moveToPath = true; //set this every time before calling StartMove
    2. yourNavMove.startPoint = 0; //desired waypoint to start at, defaults to 0
    3. yourNavMove.StartMove();
     
  12. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    Danka
    is just figured out the MoveToPath was buggering it up

    my solution so far is this...before your post


    Code (CSharp):
    1. WayPointSystem.moveToPath = true;
    2.                 WayPointSystem.SetPath(WayPointSystem.pathContainer);//flasify finishing task go home
     
  13. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Sooo.. Did you figure it out then or do you need more help? I can't tell based on your reply.
     
  14. wethecom

    wethecom

    Joined:
    Jun 24, 2015
    Posts:
    75
    ohhh yeah got it thanks...
     
    Baroni likes this.
  15. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    New Release: Version 5.1

    BREAKING CHANGES! PLEASE READ CAREFULLY
    Minimum required Unity version: 5.0.0

    - removed support for Unity 4.x
    - removed bezierMove script! splineMove works on bezier paths now too
    - removed manual type selection on MoveAnimator script (automatic now)
    - added sample scene "Events" showcasing different event scenarios
    - added waypoint rename button on path managers to update waypoint names
    - waypoint rename button has a custom checkbox to ignore custom names
    - bezier handles on bezier paths are now independent by default
    - auto-align bezier points on path creation to create correct curves
    - fixed inserting bezier point at last waypoint making it unresponsive
    - fixed exception calling CalculatePath() without previous population
    - fixed Gizmo scaling getting too large, limited to max size now
    - fixed resuming Pause before seconds run out having multiple coroutines
    - fixed inconsistent Gizmo drawing on waypoint names and height
    - fixed PlayMaker action ChangeSpeed & UpdateBezierPath on every frame
    - DOTween updated to 1.1.135

    Upgrade guide:
    - Since bezierMove scripts are gone, and if you have been using them in your scenes, please replace them with the splineMove movement script. Make a backup to easily compare script values during this process.

    See our roadmap for future features hitting public releases.
     
  16. yandrako

    yandrako

    Joined:
    Dec 2, 2013
    Posts:
    26
    Hi Baroni, I have some problems when I try to use the close loop option. I know some other people ask for this same problem but after reading your answers still cant have a solution. I have tried both with standard and bezier paths, trying different distances and angles between first and last waypoint and always get some twist, back-and-front undesired move.



    I also notice i get this warning precisely when moving from the last waypoint to the first again:

    DOTWEEN :: An error inside a tween callback was silently taken care of > Array index is out of range.
    UnityEngine.Debug:LogWarning(Object)


    I recreate different paths and always get the warning and the twists. I also even didnt use the close loop option and just put the first and last waypoint at the exact same position, but always get some jump or undesired twist.
     

    Attached Files:

  17. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @yandrako,

    could you please send a project that reproduces this to my email (found in the pdf docs or on our website).
     
  18. yandrako

    yandrako

    Joined:
    Dec 2, 2013
    Posts:
    26
    Done, I explain everything at the email.
     
  19. Comolokko

    Comolokko

    Joined:
    Jul 24, 2014
    Posts:
    28
    Can I use this asset to create something like Monument Valley's movement system? Character will move with mouse click. Walkable paths will be cubes like in Monument Valley and I want character to go connected paths. So I need to change it at runtime.
     
  20. sydknee11

    sydknee11

    Joined:
    Jan 20, 2016
    Posts:
    3
    Hello :) Bought the asset and it's great. The issue I'm having is figuring out what settings to use that radomize the waypoint pathing to where the character chooses to go ONLY to the nexy waypoint, or the previous.

    The current random option has the path object moving through walls for instance, to get to a waypoint 5 steps away. When if they are at say, waypoint 3, I want them to randomly choose to go to either waypoint 2 or 4.
     
  21. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @Comolokko
    Movement by mouse click isn't something SWS is capable of when using predefined paths, because that would require some kind of pathfinding logic. However, you could work around this by placing a collider at the position where your character should stop. Creating paths at runtime is supported, although you need to do the pathfinding logic - where the path should be - yourself in order to place waypoints at the desired positions.

    @sydknee11
    If you want to randomize the movement just by one waypoint to the left or right, using the random type won't be a feasible solution. It's better to directly control the movement by events and triggers. So if the object should follow it's path, just resume the movement and let it stop at the next waypoint via events. If it should move back instead, toggle the reverse option and start at the current waypoint (startPoint variable). On top of that, have your random function which selects one of these options.
     
  22. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Hi,

    I wanted to ask, is there a way to make this work with Third Person Controller by opsive. The character moves along the waypoint but doesn't animate the character.
     
  23. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @namdo,

    our MoveController Mecanim controller is driven by our MoveAnimator script. The script manipulates speed and direction variables in the controller which then play the corresponding animations. If you would like to have animations being played in your own or 3rd party controllers, you have to either add those variables in your controller and connect them to animation states or modify the MoveAnimator script.
     
  24. EvilEliot17

    EvilEliot17

    Joined:
    Apr 12, 2014
    Posts:
    9
    Hi nice job this is a great tool, i'm generating my path dynamically at runtime and everything works fine, but when i don´t know how to add events at runtime for each new waypoint. do you have an example ?? o can you give me an idea. thanks in advance.
     
  25. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @EvilEliot17, please have a look at example scene 7, sample 6 "Add Event". The sample adds an event at the second waypoint at runtime. Optionally you could also place a collider on the path and have that invoke a method on your script (example scene 5, sample "Method on Path".
     
  26. EvilEliot17

    EvilEliot17

    Joined:
    Apr 12, 2014
    Posts:
    9
    got it thanx a lot
     
  27. Comolokko

    Comolokko

    Joined:
    Jul 24, 2014
    Posts:
    28
    Hi, anyone knows how to reverse movement at runtime? I tried checking splineMove reverse but didn't work. Another question is can I unlink or link waypoints to eachother?
     
  28. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @Comolokko,

    please have a look at this thread for reversing movement at runtime. It works for waypoints only. A method to call at any time during runtime which reverses movement does not exist but is planned for an update (see our roadmap).

    What do you mean by connecting waypoints? Something along the lines of interceptions?
     
  29. Comolokko

    Comolokko

    Joined:
    Jul 24, 2014
    Posts:
    28
    Hi @Baroni thanks for response. I mean like random movement from waypoint 1 to waypoint 5 and 5 to 2 but not random. It can only follow the line between waypoints something like in the attachment.
     

    Attached Files:

  30. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Hi Baroni, I'm having a bit of an issue starting a waypoint with a trigger. I keep getting this error on the trigger script. "unity object not set to an instance of an object"

    This is my script.

    Code (CSharp):
    1. public class WayPoTrigg : MonoBehaviour {
    2.  
    3.     public bool wayPointer;
    4.     public GameObject Aedan;
    5.  
    6.     private splineMove myMove;
    7.  
    8.     // Use this for initialization
    9.  
    10.  
    11.  
    12.         void Awake ()
    13.         {
    14.             myMove = gameObject.GetComponent<splineMove>();
    15.  
    16.  
    17.         }
    18.     void Start ()
    19.     {
    20.             myMove.StartMove();
    21.             myMove.Pause();
    22. //            myMove.Resume();
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update ()
    28.  
    29.         {
    30.  
    31. //            if (myMove.tween == null || myMove.tween.IsPlaying())
    32. //                return;
    33.     }
    34.  
    35.         void OnTriggerEnter (Collider coll)
    36.         {
    37.             if (coll.gameObject.tag == "Player")
    38.             {
    39.                 myMove.Resume();
    40.  
    41.             }
    42.              
    43. }
    44. }
    The error points to myMove.Start();
     
    Last edited: Mar 1, 2016
  31. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @Comolokko Thanks for the drawing. Honestly I wouldn't use PathManagers and our movoment script for that. You could simply create a new script with a gameobject list, assign waypoints and attach that script to waypoint gameobjects. Then let your script choose one of these waypoints and create a separate tween. Repeat at tween completion.

    @namdo I don't think that you've attached a splineMove script to waypoints (nor should you do that) so the reference to it is null? You have to get the reference of the colliding object and do any modifications in the OnTriggerEnter method.
     
  32. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200

    I actually have. I attached the script to the collider so that when I touch the collider, it starts the waypoint on the other character.

    When I attached the script to the character, the error went off. However the collider didn't trigger the waypoint.
     
  33. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @namdo It is very difficult for me to follow your description. Objects with a splineMove script are meant to move - waypoints should not move - so they are not designed to have a splineMove script.

    I don't know what you mean by "collider didn't trigger the waypoint" either, it doesn't make much sense to me. Did you have a look at the events example scene which has a sample for events on waypoints (which reacts to a character touching a collider)?
     
  34. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Im looking at the example now. What I meant was i have a character I want to follow the waypoints. I put the spline move script on him.

    I had another gameobject which has the collision script. I was trying to use that gameobject to trigger the character to follow the waypoint, but that didn't work.

    But when I put the collision script on the character with the spline move, the error went away.
     
  35. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Because your WayPoTrigg script is accessing a splineMove reference in Awake and Start, which is only attached to your character. That's what I've said earlier.
     
  36. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Ok. I looked at the example and it doesn't really help what i'm trying to do. Maybe I just need some little explanation please.

    What I was trying to do before was trigger a character to move with on a set waypoint path using SplineMove. The character I want to move has the spline move script attached to him.

    The trigger itself which is an empty gameobject has a script which is supposed to trigger the other character to follow the waypoint path.

    This is the initial script i wrote. I used your camera move example script.

    Code (CSharp):
    1.  
    2.  
    3. {
    4.  
    5.  
    6.     private splineMove myMove;
    7.     public GameObject trigg;
    8.  
    9.     //get references at start
    10.     //initialize movement but don't start it yet
    11.     void Start()
    12.     {
    13.         myMove = gameObject.GetComponent<splineMove>();
    14.         myMove.StartMove();
    15.         myMove.Pause();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update ()
    20.  
    21.         {
    22.  
    23.     }
    24.  
    25.  
    26.         void OnTriggerEnter(Collider other)
    27.         {
    28.         if (other.gameObject.tag == "Player") {
    29.             myMove.Resume();
    30.         }
    31.  
    32.     }
    33.  
    34.  
    35. }
    I get this error on the trigger script "NullReferenceException: Object reference not set to an instance of an object" on line 22 which is myMove.Start()

    However like you mentioned, If i add this trigger script to the character with the spline move script, that error goes away but the trigger of course doesn't work.
     
  37. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    As you've wrote the script right now, it won't work for both cases (and I've mentioned why in this post).
    Case 1: you have attached your script to the empty waypoint gameobject. In Start(), it tries to get the splineMove script of itself - the empty waypoint gameobject, which does not have a splineMove script -> null reference exception.
    Case 2: you have attached your script to the character, which has a splineMove script -> no null reference exception. But in OnTriggerEnter(), you check for the "Player" gameobject (itself) -> does not work.

    So either you get the splineMove script of the Player gameobject (case 1) or you check for the collider (case 2).

    Solution for case 1, your script is attached to the empty waypoint gameobject:

    Code (csharp):
    1. private splineMove myMove;
    2. public GameObject player; //assign in inspector!
    3.  
    4. void Start()
    5. {
    6.    myMove = player.GetComponent<splineMove>();
    7.    myMove.StartMove();
    8.    myMove.Pause();
    9. }
    10.  
    11. void OnTriggerEnter(Collider other)
    12. {
    13.     if (other.gameObject == player)
    14.     {
    15.           myMove.Resume();
    16.     }
    17. }
    Solution for case 2, your script is attached to the Player gameobject:

    Code (csharp):
    1. //Start() method stays the same
    2.  
    3. void OnTriggerEnter(Collider other)
    4. {
    5.     if (other.gameObject.tag == "YourColliderName")
    6.     {
    7.           myMove.Resume();
    8.     }
    9. }
     
  38. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Ok it works perfectly, thank you. I see my mistake was using, gameObject.GetComponent instead of player.GetComponent.

    I honestly wouldn't have been able to figure that out by myself.
     
  39. Taufreak

    Taufreak

    Joined:
    Mar 8, 2016
    Posts:
    2
    Hello,

    I have a box mesh and I set a spline path above my terrain, everything works find with the box following the path.
    But I have a question, now I want to add gravity to the box using rigidbody, apparently the rigidbody gravity does not have any effect. The box continue to follow the path without falling down.

    How can I use rigidbody gravity to only apply on the y-axis of the box while still following the spline path? Is it possible?
     
  40. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    Hi @Taufreak,

    tween movement updates the transform position of the moving object directly and, just like you know it from animations, ignore any force applied externally. This means that your physic changes get overwritten by DOTween every frame. You have two options to get the effect you're after:

    - do not use physics and use absolute position updates instead. For example you could raycast against the terrain and set the box y position to the point it hit. Apply the position in LateUpdate(), because this is after an DOTween frame update.
    - or, have the movement script on an empty gameobject and make the box a child of it. Then try to apply your rigidbody changes to the child locally, while the parent still follows the path.
     
  41. Redrag

    Redrag

    Joined:
    Apr 27, 2014
    Posts:
    181
    Is the path fixed in global space? I need to move my character around on a platform and can't see a way of doing that?
     
  42. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    You would be looking at local paths then. Please the the "Local" sample in the advanced example scene.
     
  43. Redrag

    Redrag

    Joined:
    Apr 27, 2014
    Posts:
    181
    Thanks - that seems to work. I thought I had tried that! Another question: Do any of the scripts have the ability to work out the quickest route from one point to another (if you had a circular route)?
     
    Last edited: Mar 8, 2016
  44. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @Redrag unfortunately not. You could iterate over the path waypoints yourself and do a distance check, or if you have a moving object on all paths, get the movement script and call tween.PathLength() (that's a DOTween method).
     
  45. Redrag

    Redrag

    Joined:
    Apr 27, 2014
    Posts:
    181
    Thanks for that. A few more questions:
    1) It looks like I will need to update the paths at runtime, as switching paths causes rotation issues etc. Could you tell me how to force an update after moving a waypoint.
    2) Could you tell me how to use reverse to change direction while in the middle? I seem to have to stop and start which resets the character to the end of the path..
     
  46. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @Redrag
    1) Tweens cannot be updated while playing. They have to be recalculated and initialized after moving a waypoint. You need to stop the movement script and restart it from the last position (currentPoint and startPoint variables).
    2) Same as 1), reversing needs to recalculate the path and therefore a reinitialization too. You can set the startPoint variable again to start from a different waypoint on the path.

    I will put these things on our roadmap for runtime changes to be included in a later version.
     
  47. markfrancombe

    markfrancombe

    Joined:
    May 26, 2009
    Posts:
    155
    Im probably doing something silly.. I cant make a Path because I get the error "No path name defined"... but when I try to click in Enter Path Name name slot, nothing happens, I cant write there... Is this a bug or have I set up something wrong...

    Weirdly this isnt the first time Ive used SWS, and Im sure I didn't have this problem before,. however.. that was U4 now Im on U5.2.1
     
  48. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @markfrancombe are you using the latest version of SWS? There has been a fix for the issue you're describing few versions ago.
     
  49. markfrancombe

    markfrancombe

    Joined:
    May 26, 2009
    Posts:
    155
    @Baroni Maybe not, ... How do I find out what version I have? BUT.. I did manage to find a work around.... weirdly PASTING into the area worked... so I copyed the path name from somewhere else...

    I have another problem now, My character is Idling as they speed around the path, but not moving to the walk/run animations, but I guess thats not really a SWS issue?

    Mark
     
  50. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,270
    @markfrancombe the Changelog.txt file in the SWS folder shows the current version at the top.

    If you would like to use our MoveAnimator script on your character, you have to make sure that the animator controller you are using on the character has a "speed" and "direction" variable with transitions between the different animations. The MoveAnimator script then changes the variables and tries to notify your animator controller of changes, which plays back the corresponding animation.