Search Unity

Flythrough example using Bezier's and waypoints

Discussion in 'Scripting' started by Sam at FPS, Sep 2, 2009.

  1. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    Hi all,

    I've been playing around with creating a flythrough for the title screen of my game and thought I would share it. I'm sure there are other examples out there but I couldn't find exactly what I wanted so I did it myself. Maybe this will be helpful to someone.

    The flythrough consists of a empty game objects, one of which has the FlythroughController script attached and all the others (waypoints) have the FlythroughWaypoint script attached.

    Using Bezier curve things the result is a nice smooth path through the waypoints. Using Gizmos the waypoints and resultant path are visible in the Scene view and can be modified visually.

    The math is done in MyBezier.cs. I originally didn't realise what I needed and implemented a N-point Bezier system, but it only uses four points at a time.

    The FlythroughController is the object that moves, so for the example project here I just added the SmoothFollow script to the camera and make the flythrough controller the target.

    For extra bling, try running the Game with Gizmos on. Looks like a rollercoaster :)
     

    Attached Files:

  2. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Would you mind checking the file please, I tried to d/l it, but it came up all cryptic.

    Thanks

    -Raiden
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    looks very usefull

    Thank you for contributing
     
  4. cosmicmesh

    cosmicmesh

    Joined:
    Mar 19, 2009
    Posts:
    61
    This is very cool. Thanks for sharing!

    @Raiden -- it appears to be showing the unity package (binary) as a text file. If you save it, and then import the package into Unity, it works just fine.
     
  5. WiseGuy

    WiseGuy

    Joined:
    Mar 1, 2009
    Posts:
    33
    Very Useful Package.
    Thanks.
     
  6. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    I needed this. Thank you.
     
  7. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    Cheers, hope you can get some use out of it.

    One thing that needs fixing is that if you add another waypoint it is added to the end of the list of waypoints (in the hierarchy). That order is the order that the script pulls them out using GetComponentsInChildren().

    I've tried to drag and drop the waypoints into different positions in the Hierarchy but it seems to do nothing. Is there a way to reorder items in the hierarchy?
     
  8. ElmarKleijn

    ElmarKleijn

    Joined:
    May 11, 2009
    Posts:
    87
    Thanks for sharing! I have not tried it yet, but it might be exactly what I was going to need next week ;).

    About your hierarchy: I think the documentation says not to rely on that order, because it might not be always correctly retrieved using any of the enumeration options. Or possibly I am confused with the actual Start() function being called.

    Anyway, what about using an public array variable which you have to fill with the objects you want to use? And array order is the fly order then. Just a random thought..
     
  9. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    Re: Almar

    Yeah that would probably work well.

    It would still have the problem of wanting to insert a waypoint in the middle of the others, because you would have to manually shift all the others down wouldn't you? But I think that is a problem common to all publicly (inspector) editable arrays. It would be nice if you could click on an element of the array and choose insert or something.
     
  10. Philip072

    Philip072

    Joined:
    Jul 3, 2009
    Posts:
    6
    Hi thanks for a great script. All is working fine, except for when I enable the controller script after startup, the controller
    starts at random positions along the curve.If I enable everything at startup,everything is fine. Any ideas on what is causing this?
     
  11. Sam at FPS

    Sam at FPS

    Joined:
    Sep 1, 2009
    Posts:
    80
    If you look at the script, in the Update() method there is this

    Code (csharp):
    1. float currentTime = Time.time % SecondsForFullLoop;
    Time.time is the time since the game started.

    If you make a member called ElapsedTime or something and initialise it to zero, then put in update...

    Code (csharp):
    1. ElapsedTime += time.deltaTime;
    2. float currentTime = ElapsedTime % SecondsForFullLoop;
    it should then only start running when it's enabled (ie when Update is called).
     
  12. Philip072

    Philip072

    Joined:
    Jul 3, 2009
    Posts:
    6
    Awsome!!Thanks very much,it solved the problem! :D :) :idea:
     
  13. michaelvoigt

    michaelvoigt

    Joined:
    Oct 16, 2007
    Posts:
    17
    This is extremely useful for so many applications.

    thanks!
     
  14. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    Hi Sam,

    Do you know a way of easierly allowing for variable speed.

    The bezier path is calculated at initialization based on the SecondsForFullLoop value.

    I want to be able to control the speed of my camera at any point as it moves around the full path.

    Any help greatly appreciated, and thanks again for a great bit of code!
     
  15. Troy-Dawson

    Troy-Dawson

    Joined:
    Nov 2, 2009
    Posts:
    120
    Do you know a way of easierly allowing for variable speed.

    I've been working on this spline code, here's my second attempt at cleaning it up.

    dt in SplineController determines where on the curve your next position will be (0.0 start and 1.0 the next-to-last-point).
     

    Attached Files:

  16. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    Wow, thanks Troy!

    I have downloaded and had a play with it.

    I added the following code, after the dt += Time.deltaTime; line.

    Code (csharp):
    1.  
    2.         if (Input.GetAxis("Mouse ScrollWheel") > 0){
    3.             path_time = path_time - 1.0f;
    4.             if (path_time < 0.0f ) {
    5.                 path_time = 0.0f;
    6.             }
    7.     }
    8.        
    9.         if (Input.GetAxis("Mouse ScrollWheel") < 0){
    10.             path_time = path_time + 1.0f;
    11.             if (path_time > 20.0f) {
    12.                 path_time = 20.0f;
    13.             }
    14.         }
    15.  
    16.  
    And it enables me to change the speed of the camera as it moves around the waypoints, but it is very jerky.

    Any ideas how to make it nice and smooth?

    Thanks again!
     
  17. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Greg,

    Don't use a set value like 1. You don't know how often this script is being called so 1, will be added at different times - thus the jitter.

    Instead of:
    Code (csharp):
    1.  
    2. path_time = path_time + 1.0f;
    3.  
    try:
    Code (csharp):
    1.  
    2. path_time += Time.deltaTime;
    3.  
    if this is not enough (Scroll speed is too slow) then add a multiplier (value becomes larger but still dependent on time elapsed since last time script was called.
    Code (csharp):
    1.  
    2. path_time += Time.deltaTime * 10;
    3.  
     
  18. rakudoor

    rakudoor

    Joined:
    Nov 12, 2009
    Posts:
    16
    Thank you very much ,this is what I am looking for.

    But when I use the code in Unity For iPhone 1.5.1 ,it seems that there are some error like this :
     

    Attached Files:

  19. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    I have modified my code as follows:

    Code (csharp):
    1.  
    2. path_time = path_time + Time.deltaTime * 20;
    3.  
    And it almost works as desired, the jerkiness is much less, but it still exists, and when I scroll the mouse wheel the camera jerks back slightly, before adjusting its speed around the waypoints.

    What I am trying to do is have a camera or game object follow a preset waypoint path smoothly, with the ability to adjust the speed of the camera or object.

    If anyone can help me get this working, I would be so grateful!
     
  20. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Greg,

    What I used (and it worked quite nice if I remember - don't need it anymore however) is some code I found where a group of people were trying to emulate the controls from WoW.

    Link is here:

    http://forum.unity3d.com/viewtopic.php?t=18073&highlight=wow+camera

    might be a valuable read.

    Remember to post a solution if you find one. I will take a look at my code if I can find it and post it here as I think my scroll code worked fine (but it was based on the WoW code so read that too).
     
  21. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    Thanks for the info on this thread, the WoW camera script is awesome.

    However I don't understand how it will help with my problem?

    Could you explain, sorry if I am being thick...
     
  22. AmazingInt

    AmazingInt

    Joined:
    Dec 7, 2009
    Posts:
    157
    How would I adjust the script to only use waypoint 0 and finish the path at waypoint x without returning back to 0?
     
  23. ram4nd

    ram4nd

    Joined:
    Dec 14, 2009
    Posts:
    5
    Nice job. Many people find it useful.
     
  24. Sal

    Sal

    Joined:
    Dec 2, 2009
    Posts:
    29
    I'll be looking into your code for this pathing system for a fresh look.
    On a side note, I've been working on a likewise system, but the emphasis first on creating a user/developer friendly way of adding/inserting points in the path.
    For this I've used an editor script overlaying my usual inspector. Here adding a button, which allows me to add a new waypoint. All the array management is done internally, so you dont have to gimmick with it.

    It is still a work in progress though. I'm planning on re-writing everything from scratch, since my understanding of Unity is now slightly better. And with that also implementing a bezier spline, rather than straight point-to-point spline.

    I will post again when significant progress is made.
     
  25. eurosat7

    eurosat7

    Joined:
    Feb 12, 2010
    Posts:
    3
    I really appreciate your work - keep it up. Just wanted to tell you: I'm following your progress and am shivering in exitement for some really cool code to come. ;)
     
  26. Chowderman

    Chowderman

    Joined:
    May 21, 2009
    Posts:
    92
    Hey guys, just wanted to chime in and say that so far this is most useful bezier script I have found! cheers!

    So now my question: I'm trying to get a particle emitter moving on this path. Right now, it's taking the same amount of time to go between waypoints, independant of their distance. This makes for some irregular particle emissions, as parts of the curve with less waypoints cause the emitter to travel faster. Since I am trying to make a dotted line, is there any way to set constant speed to the object traveling down the spline no matter the distance of the waypoints?

    Please say yes!

    - Chowder
     
  27. howardmoon

    howardmoon

    Joined:
    Jun 28, 2010
    Posts:
    3
    Sam just wanted to say a big thanks for sharing this, it works really well for my game, adding a nice level of polish. Cheers.
    Pete.
     
  28. kev_poole

    kev_poole

    Joined:
    Feb 14, 2011
    Posts:
    7
    HI great scripts

    I have a problem with the scripts, overall they work very well untill i disable them in inspector and use another script to re-enable the waypoints.

    i pass the gameobject to my scripts like so.

    public GameObject ProcessRoute;

    then i enable them like so...

    ProcessRoute.active = true;
    ProcessRoute.SetActiveRecursively(true);

    now the problem is it thows the following error and does not move the camera along the predefined path

    ArgumentOutofRangeException: Index is less than 0 or more than or equal to list count.

    basicly i need to create fly waypoints that are initaly disabled from inspector and have them enabled in script so that the fly waypoints begin when i enable them.

    any ideas anyone?
     
  29. LuxUnity

    LuxUnity

    Joined:
    Sep 29, 2010
    Posts:
    717
    Very nice, thank you!
     
  30. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I know this thread is very old, but is there a chance that this package could be fixed to work with Unity 3.5? I get a bunch of errors and the camera doesn`t move. There is a script missing.
     
  31. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    It seems to be useful, thank you. Can you upload this one in the UnityWiki?
     
  32. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    hi all
    very thanks for your script.

    my problem with it is:
    when i assign 100 for secondsloop then it have very jumps.
    how can smooth it?

    thankful.
     
  33. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    hi all
    how can apply dynamic speed?(dynamically increase and decrease speed)?
    thankful.
     
  34. patrick13

    patrick13

    Joined:
    Dec 29, 2013
    Posts:
    13
    Hello
    Firstly your script is what I'm looking for months.
    Then I do not know how the script
    In my work I make models of traffic.
    I need a script to move cars along a path.
    In Maya, I make a spline made ​​of regularly spaced objects (spheres).
    an object 10 meters, 10 km. So 1000 objects.
    I then exported to FBX.
    In your script, you put your pins manually.
    I would do it automatically with my spheres
    By public variables in the script, I would:
    1 give the path
    2 give the speed in km per hour
    3 tell the car how it begins its journey landmark
    (Example with 2 cars, the first begins at 1° sphere and the second car starts at 5°)
    Once at the last point, the car reappeared in his room immediately start marker
    thank you for your help if you do it
     
    Last edited: Dec 30, 2013
  35. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Hi, can anyone help me figure out how to draw path same like with OnGizmos, but ingame, with primitives for example.
    I've done instatiation
    Code (csharp):
    1.  
    2. spline = new Spline(GameObject.Find("Path"));
    3.        
    4.         for (float t = 0; t < 1.0F; t += 0.025f)
    5. Instantiate(SpherePrefab, spline.GetPosition(t), Quaternion.identity));
    6.  
    but cant make it to update positions when the waypoint position has been change, like in Gizmos