using UnityEngine; using System.Collections; public class movebetween : MonoBehaviour { public GameObject[] points; //public GameObject end; public float distance; public float changeDis; private Vector3 startPos; private Vector3 endPos; public int count = 0; public bool useCustomRotation = false; private Rigidbody rb; void Start(){ rb = gameObject.GetComponent (); startPos = points[count].transform.position; endPos = points[count+1].transform.position; distance = Vector3.Distance (startPos, endPos) / Vector3.Distance (gameObject.transform.position, startPos); } // Update is called once per frame void Update () { //transform.position = startPos + (endPos - startPos) / distance; if (!useCustomRotation) { Vector3 lookAtTarget = endPos; lookAtTarget.y = transform.position.y; transform.LookAt (lookAtTarget); } } void FixedUpdate(){ if (Vector3.Distance (gameObject.transform.position, startPos) <= changeDis && count != 0) { count--; startPos = points[count].transform.position; endPos = points[count+1].transform.position; } if (Vector3.Distance (gameObject.transform.position, endPos) <= changeDis && count + 1 != points.Length) { count++; startPos = points[count].transform.position; endPos = points[count+1].transform.position; } distance = Vector3.Distance (startPos, endPos) / Vector3.Distance (gameObject.transform.position, startPos); Vector3 newPos = gameObject.transform.position; newPos.x = startPos.x + (endPos.x - startPos.x) / distance; newPos.y = startPos.y + (endPos.y - startPos.y) / distance; rb.MovePosition (newPos); } }