Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Train to follow path?

Discussion in 'Scripting' started by dom_g, Apr 9, 2010.

  1. dom_g

    dom_g

    Joined:
    Apr 29, 2009
    Posts:
    54
    Hi, I'm pondering on how to get a train to follow a path that will rotate it in the right direction also. Does anyone have any suggestions? I was thinking of using a spline as a path, but I'm not sure if this would work. Any help is greatly appreciated.
     
  2. mediahaze

    mediahaze

    Joined:
    Dec 1, 2009
    Posts:
    22
    Hey,

    There are a number of ways but I would do the following just because its easy, quick and visual.

    A) Create a waypoint object to represent your path. Have a sphere object be a component to it as well as have a script called FindNextWayPoint or something similar.

    A) Create a series of waypoint objects that represent your path. Name the something sequential(prefix_number) ie wp1,wp2,wp3.

    b) Write the FindNextWayPoint script that searches for the next waypoint given its name and then calculates the direction vector towards it(which you then store). This will let wp1 looks for wp2 and so on. Add an extra bool parameter that checks to see if it should toggle off the renderer of the object at Start(). I would finally add an NextWayPoint GameObject that stores this next object.


    c) Write a path following script for your train. Give it a parameter to set for the first node. Have that script also perform the moving ect. When you object reaches a new node, have it set its transform.forward direction to be the direction of the next node you stored in the node script. You may need to do some interpolation here to make the turns smooth depending on the number of waypoints you provide. You can do this and grab your next destination from this node.

    Hope that helps.
     
  3. Grady Lorenzo

    Grady Lorenzo

    Joined:
    Jan 18, 2010
    Posts:
    407
    I tried doing a similar thing for an experimental MMO at one time, except it was a cruise ship moving through an ocean.

    A solution would be animating your train in your modeling prog. 3Ds Max has a nice setup for this so as to allow said object to follow a spline (line constraint), and plenty of options to go with it. Then simply export the model with the animation to Unity.

    Once there, you will have to check "Animate Physics". Then go nuts.
     
  4. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    419
  5. dom_g

    dom_g

    Joined:
    Apr 29, 2009
    Posts:
    54
    Thanks for advice, I've gotten as far as to get the train moving and affect the speed. At the moment I'm looking at the Car Race tutorial but can't get the waypoint system to work, within this. I would like to use animations for the track, it would be really much easier for me, except for the fact that I need to control speed and dynamically choose paths. If i'm right that might pose a problem in terms of switching directions. The waypoint system is exactly what I'm after, but at the moment I just can't get my head around it.

    Cheers
     
  6. dom_g

    dom_g

    Joined:
    Apr 29, 2009
    Posts:
    54
    Hi, I'm trying to adapt the car tutorial script, but can't get the moving to the waypoints working with my already implemented moveDirection, here is the code I am using, any ideas/suggestions? Thanks for viewing.

    Code (csharp):
    1.  
    2. var trainPlayer : Transform;
    3. var ambientSpeed : float = 20;
    4. var moveDirection = Vector3.zero;
    5. private var activeWaypoint : trainWaypoint;
    6.  
    7. //initialise the waypoint before the train starts moving
    8. function Start (){
    9.  
    10.     activeWaypoint = trainWaypoint.start;
    11.  
    12. }
    13.  
    14. //tracks the target position relative to the train. Calculates the angle to take.
    15. function UpdateWithTargetPosition (target : Vector3){
    16.  
    17. relativeTarget = transform.InverseTransformPoint (target);
    18. targetAngle = Mathf.Atan2 (relativeTarget.x, relativeTarget.z);
    19. targetAngle *= Mathf.Rad2Deg;
    20.  
    21.  
    22. }
    23.  
    24. //update type used for physics based objects
    25. function FixedUpdate(){
    26.  
    27.         targetPosition = activeWaypoint.CalculateWaypointPosition (transform.position);
    28.         UpdateWithTargetPosition (targetPosition);
    29.        
    30.         moveDirection = new Vector3 (1, 0, 0);
    31.         moveDirection = transform.TransformDirection (moveDirection);
    32.         moveDirection *= ambientSpeed;
    33.        
    34.         //moves the train using the character controller
    35.         var trainController : CharacterController = GetComponent (CharacterController);
    36.         //var moveTrain = trainController.Move(moveDirection * Time.deltaTime);
    37.         var moveTrain = trainController.Move(moveDirection * Time.deltaTime);
    38.        
    39.         //prints the speed we are going at and the target position of the waypoint
    40.         print (moveDirection.x);
    41.         print (targetPosition);
    42.    
    43.        
    44.  
    45.         if (Input.GetKey ("b")){
    46.        
    47.             if (ambientSpeed > 20){            
    48.        
    49.        
    50.                 ambientSpeed --;
    51.        
    52.         }
    53.        
    54.         }
    55.        
    56.         if (Input.GetKey ("c")){
    57.        
    58.             if (ambientSpeed < 121){  
    59.        
    60.            
    61.                 ambientSpeed ++;
    62.                                                
    63.                 }
    64.        
    65.         else {
    66.        
    67.             if (ambientSpeed > 120){
    68.        
    69.  
    70.        
    71.    
    72.        
    73.         }
    74.         }
    75.         }
    76. }

    And the waypoints:

    Code (csharp):
    1.  
    2.  
    3.  
    4.  
    5. static var start : trainWaypoint;
    6. var next : trainWaypoint;
    7. static var junction : trainWaypoint;
    8.  
    9. var isStart = false;
    10. var isJunction = false;
    11.  
    12.  
    13.  
    14. function CalculateWaypointPosition (position : Vector3){
    15.  
    16.  
    17.         return transform.position;
    18.  
    19.         //print (transform.position);
    20.  
    21.  
    22. }
    23.  
    24.  
    25. function Awake(){
    26.  
    27.  
    28. if (isStart)
    29.     start = this;
    30.  
    31. }
    32.  
    33.  
    34. function OnDrawGizmos(){
    35.  
    36. Gizmos.DrawIcon (transform.position, "Gizmos/WayPointMarker.png");
    37.  
    38. }
    39.  
    40.  
    41. function OnDrawGizmosSelected (){
    42.  
    43. if (next) {
    44.  
    45.     Gizmos.color = Color.red;
    46.     Gizmos.DrawLine (transform.position, next.transform.position);
    47.  
    48.  
    49. }
    50. }
    51.  
    52.  
     
  7. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,823
    Or, you could do something so easy, it's insidious.

    You could just download the FPS Tutorial project and take the waypoint system. :)

    Problem solved.
     
  8. thomastheunityengine

    thomastheunityengine

    Joined:
    Nov 6, 2017
    Posts:
    1
    What's the FPS Tutorial project? What does FPS stand for?
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    There are lots of tutorials around on youtube, which I think is a better solution.
     
  10. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Woooooow. 7 year necro for unrelated comment!

    Thomas - Google does help but noting there could be some confusion, in Unity/gaming terms, FPS can mean Frames per second or First person shooter. In this instance it means the latter. Unity does a large range of incredibly helpful tutorials (which I am becoming more aware that people do not seem to use for some reason. I still look through them!). They are accessible here: https://unity3d.com/learn/tutorials
    I highly recommend them...

    I am not actually sure what Andrew was referring to re: FPS tutorial project. There used to be a simple single player FPS tutorial but the only current one is a mulitplayer only.
    For waypoints: A google search is all you need. "Unity waypoint system" yields a range of assets, video tutorials and forum posts that will set you on your way.
    For train specific, the first asset ($15) in the previous search looks like it may be functional (bezier curve based waypoints)
    Much more expensive but quite robust is the Megashapes asset; train specific:


    Alternatively the free iTween would allow you to animate along a splined path
    https://assetstore.unity.com/packages/tools/animation/itween-84

    And at the basic level, fire7side's link gives you all you need. The illusion of smooth curves can even be achieved by adding waypoints.

    Google and research candidate solutions for your specific need.
     
  11. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,823
    So, the FPS Tutorial project was a demo project that taught you how to make a First-Person Shooter (FPS) game. I'm not sure it's still around, and I'm not even sure that the Wayback Machine would have an archive of the ZIP. It was around way back in Unity 2.6+, back when I started using Unity after college.

    I think I'll try to find it again one of these days. It had an awesome waypoint system that I used in a number of ways, and some other really useful code assets.