How do i make a 3D object move along a set path when the player comes in contact with it
I'm very new to unity and I'm trying to have my train and boat move along a set path to their destinations. I'm not sure how to have the player activate the object or have the train move along the set path.
public class MoveObjects : MonoBehaviour { public Transform target; public float speed; Transform Waypoint1; Transform Waypoint2; Transform Waypoint3; Transform Waypoint4; Transform currWaypoint; int wpNum=1;
// Use this for initialization
void Start () {
Waypoint1 = GameObject.Find("Waypoint1").GetComponent<Transform>();
Waypoint2 = GameObject.Find("Waypoint2").GetComponent<Transform>();
Waypoint3 = GameObject.Find("Waypoint3").GetComponent<Transform>();
Waypoint4 = GameObject.Find("Waypoint4").GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
float step = speed * Time.deltaTime;
if (wpNum == 1)
{
if(transform.position!= currWaypoint.position)
{
transform.position = Vector3.MoveTowards(transform.position, currWaypoint.position, Time.deltaTime);
}
}
else
{
currWaypoint = Waypoint2;
wpNum++;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612121727im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Drag Object from left to right ONLY 2 Answers
Object movement algorithm 0 Answers