This lesson covers how to create a script that can be attached to any object with a Rigidbody andhave the object move to any GameObject items on a Scene.
Create the following script PathFinder
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Pathfinder : MonoBehaviour { public Transform[] locations; public float speed; private int targetLocationIndex; void Start() { } void Update() { if (transform.position != locations[targetLocationIndex].position) { Vector3 nextLocation = Vector3.MoveTowards(transform.position, locations[targetLocationIndex].position, speed * Time.deltaTime); GetComponent<Rigidbody>().MovePosition(nextLocation); } else targetLocationIndex = (targetLocationIndex + 1) % locations.Length; } }
Attach the script to the object (in this example the Main Camera).