using UnityEngine;
using System.Collections;
public class LookForPath : MonoBehaviour
{
public Transform[] TargeArr;
public bool isBool=false;
int i = 0;
void OnMouseDown()
{
isBool = true;//isBool设为真
}
void Update()
{
IsOn();
}
void IsOn()
{
if (isBool)//判断isBool是否为真
{
if (Vector3.Distance(transform.position, TargeArr[i].position) > 0.1f)//判断两者间的距离是否大于0.1f
{
transform.position = Vector3.MoveTowards(transform.position, TargeArr[i].position, 0.5f *Time.deltaTime);
}
else
{
if (++i == TargeArr.Length)
{
i = 12;
}
}
}
}
}
注:
*要移动的物体需要加BoxCollider组件,OnMouseDown()事件才会响应。
运行结果:点击尺子,尺子按照预先设定的路线进行移动