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.

61 lines
1.6 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class movebetween : MonoBehaviour {
  4. public GameObject[] points;
  5. //public GameObject end;
  6. public float distance;
  7. public float changeDis;
  8. private Vector3 startPos;
  9. private Vector3 endPos;
  10. public int count = 0;
  11. public bool useCustomRotation = false;
  12. private Rigidbody rb;
  13. void Start(){
  14. rb = gameObject.GetComponent<Rigidbody> ();
  15. startPos = points[count].transform.position;
  16. endPos = points[count+1].transform.position;
  17. distance = Vector3.Distance (startPos, endPos) / Vector3.Distance (gameObject.transform.position, startPos);
  18. }
  19. // Update is called once per frame
  20. void Update () {
  21. //transform.position = startPos + (endPos - startPos) / distance;
  22. if (!useCustomRotation) {
  23. Vector3 lookAtTarget = endPos;
  24. lookAtTarget.y = transform.position.y;
  25. transform.LookAt (lookAtTarget);
  26. }
  27. }
  28. void FixedUpdate(){
  29. if (Vector3.Distance (gameObject.transform.position, startPos) <= changeDis && count != 0) {
  30. count--;
  31. startPos = points[count].transform.position;
  32. endPos = points[count+1].transform.position;
  33. }
  34. if (Vector3.Distance (gameObject.transform.position, endPos) <= changeDis && count + 1 != points.Length) {
  35. count++;
  36. startPos = points[count].transform.position;
  37. endPos = points[count+1].transform.position;
  38. }
  39. distance = Vector3.Distance (startPos, endPos) / Vector3.Distance (gameObject.transform.position, startPos);
  40. Vector3 newPos = gameObject.transform.position;
  41. newPos.x = startPos.x + (endPos.x - startPos.x) / distance;
  42. newPos.y = startPos.y + (endPos.y - startPos.y) / distance;
  43. rb.MovePosition (newPos);
  44. }
  45. }