• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 10, 2016 at 08:04 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by skgamer · Dec 03, 2012 at 03:24 PM · object move

moving train

hi i have a train and railway track and stations setup i just want that my train move from one station to other and stop also can someone please help me how can i move the train

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by SohailBukhari · Feb 10, 2016 at 05:39 PM

@skgamer

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class TrackFollowingTrain : MonoBehaviour {

 public GameObject waypointContainer;
 public GameObject[] trainparts;
 public AnimationCurve startCurve;
 
 private List<Transform> waypoints;
 public List<int> currentWaypoints;
 
 public float speed = 0.1F;
 public float currentSpeed;
 private float startTime;
 public int direction = 0;
 
 // Use this for initialization
 void Start () 
 {
     for (int i = 0; i < trainparts.Length; i++) 
     {
         currentWaypoints.Add (0);
     }
     waypoints = GetWaypoints();
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
     if (currentWaypoints[0] >= waypoints.Count || currentWaypoints[0] < 0) 
     {
         direction = 0;
     } 
     else 
     {
         for(int i = 0; i < trainparts.Length; i++) 
         {
             currentWaypoints[i] = MoveTransformTowardsWaypoint(trainparts[i].transform, currentWaypoints[i]);
         }
     }
 }
 
 public void StartTrain() 
 {
     speed = 5F;
     if (currentWaypoints[0] < 10)
     {
         direction = 1;
         for (int i = 0; i < currentWaypoints.Count; i++)
             currentWaypoints[i]++;
     }
     else if (direction == 0) 
     {
         direction = -1;
         for (int i = 0; i < currentWaypoints.Count; i++)
         {
             currentWaypoints[i]--;
         }
     }
     startTime = Time.time + 2.0F;
 }
 
 List<Transform> GetWaypoints () 
 {
     // This function takes the container object for the waypoints, then finds all of the transforms in it,
     // once it has the transforms, it checks to make sure it’s not the container, and adds them to the list of waypoints.
     
     Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren<Transform>();
     List<Transform> waypoints = new List<Transform>();
     
     foreach (Transform potentialWaypoint in potentialWaypoints) 
     {
         if (potentialWaypoint != waypointContainer.transform)
         {
             waypoints.Add(potentialWaypoint);
             
             // correction for right rail, track width = 75 cm
             Vector3 correction = new Vector3(0.375F, 0.0F, 0.0F);
             potentialWaypoint.Translate(correction, Space.Self);
             
         }
     }
     return waypoints;
 }
 
 int MoveTransformTowardsWaypoint(Transform tpartTF, int currentWaypoint)
 {
     currentSpeed = speed * startCurve.Evaluate((Time.time-startTime) / 10);
     float currentStep = currentSpeed * Time.smoothDeltaTime;
     
     Vector3 movementVector = Vector3.MoveTowards(tpartTF.position,waypoints[currentWaypoint].position,currentStep);
     float distance = Vector3.Distance(tpartTF.position, movementVector);
 
     if (currentStep-distance> 0.001)
     {
         // if this increment passes the waypoint, we move towards the next waypoint instead
         currentWaypoint += direction;
         if (currentWaypoint >= 0 && currentWaypoint < waypoints.Count)
         {
             movementVector = Vector3.MoveTowards(tpartTF.position,waypoints[currentWaypoint].position,currentStep);
         } 
         else 
         {
             return currentWaypoint;
         }
     }
     
     tpartTF.position = movementVector;
     tpartTF.LookAt(waypoints[currentWaypoint]);
     return currentWaypoint;
 }

}strong text

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Feb 10, 2016 at 08:04 PM 0
Share

Little bit late there...

avatar image Abbubakar · Apr 23, 2016 at 06:09 AM 0
Share

shohail i ca set a script but the train movement way point is not smooth how can i set it smooth

avatar image SohailBukhari Abbubakar · Sep 05, 2016 at 06:34 PM 0
Share

sorry for late reply $$anonymous$$ake Collider based physics to move train , if you need train physics then tell me

avatar image viditvaswani18 · Mar 31, 2018 at 09:41 AM 0
Share

train is not moving

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make enemy chase player. Basic AI 7 Answers

How to Make a simple GUI using unity C# and Kinect 0 Answers

How to create an if statement for each element within a list C# 2 Answers

C# Rigidbody Rotation Resets 1 Answer

Help with a simple jump script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges