using UnityEngine; using System.Collections; public class RayScript2 : MonoBehaviour { Vector3 _Taget; bool _isMove=true;//移动是否结束 public float speed=1f; void Start() { } void Update () { if (Input.GetMouseButtonDown(1))//移动 { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.collider.tag == "Dimian") { _Taget = hit.point; _Taget.y = 0.5f; _isMove = false;//现在又有新的目标了继续移动吧! } } } Move(_Taget); } void Move(Vector3 _Tar) { if (!_isMove) { Vector3 v1 = _Tar - transform.position; transform.position += v1.normalized * speed * Time.deltaTime; transform.LookAt(_Tar); if (Vector3.Distance(_Tar, transform.position)<0.3f) { transform.position = _Tar; _isMove = true; } } } }