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.
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
Best Answer
Answer by SohailBukhari · Feb 10, 2016 at 05:39 PM
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
shohail i ca set a script but the train movement way point is not smooth how can i set it smooth
sorry for late reply $$anonymous$$ake Collider based physics to move train , if you need train physics then tell me