using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PointArrMove : MonoBehaviour {
public GameObject moveCube;
public List<Vector3> pArr; //重点是List类型的数组变量,可以存储连续点
private Camera _camera;
private Vector3 screenV;
public int len;
private float speed=3.0f;
private float s; //当前要移动的距离
private float dis;
private float speedX;
private float speedY;
private int moveFlag;
// Use this for initialization
void Start () {
pArr = new List<Vector3> ();
_camera = Camera.main;
//将世界坐标转换为屏幕坐标
screenV = _camera.WorldToScreenPoint (moveCube.transform.position);
}
void moveCount(){
Vector3 p = pArr [0];
Vector3 v = moveCube.transform.position; //重要:v此时是时刻在变得三维坐标,而不是固定的第一个点的位置;
float dx = p.x - v.x;
float dy = p.y - v.y;
s = Mathf.Sqrt (dx * dx + dy * dy);
float hudu = Mathf.Atan2 (dy, dx);
speedX = speed * Mathf.Cos (hudu);
speedY = speed * Mathf.Sin (hudu);
moveFlag = 1;
dis = 0;
pArr.RemoveAt (0);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 dianV = Input.mousePosition;
dianV.z = screenV.z;
//将屏幕坐标转化为世界坐标
Vector3 wv= _camera.ScreenToWorldPoint(dianV);
//将得到的世界坐标存入数组
pArr.Add(wv);
if(moveFlag==0){
moveCount();
}
}
if (moveFlag == 1) {
moveCube.transform.Translate(Vector3.right*Time.deltaTime*speedX);
moveCube.transform.Translate(Vector3.up*Time.deltaTime*speedY);
dis += Time.deltaTime*speed;
if(dis>=s){
Debug.Log (moveCube.transform.position);
//moveCube.transform.position = p;
if(pArr.Count>0){
moveCount();
}else{
moveFlag=0;
}
}
}
}
}