You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
2.1 KiB

using UnityEngine;
using System.Collections;
public class movebetween : MonoBehaviour {
public GameObject[] points;
//public GameObject end;
public float distance;
public float startDistance;
public float endDistance;
public float changeDis;
private Vector3 startPos;
private Vector3 endPos;
public int count = 0;
public bool useCustomRotation = false;
private Rigidbody rb;
public float moveForce;
void Start(){
rb = gameObject.GetComponent<Rigidbody> ();
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(){
startDistance = Vector3.Distance (gameObject.transform.position, startPos);
endDistance = Vector3.Distance (gameObject.transform.position, endPos);
if (Vector3.Distance (gameObject.transform.position, endPos) <= changeDis || Vector3.Distance (gameObject.transform.position, startPos) <= changeDis) {
moveForce = 1000;
} else {
moveForce = 0;
}
rb.AddForce (transform.forward * moveForce * Time.deltaTime);
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);
}
}