标签:sep oat distance stc mesi new 方式 摄像机 time
private bool IsMove;//移动
//鼠标双击的参数(第一种方式的参数)
private float delay = 0.5f;
private float firstClickTime = 0;
private bool oneClick = false; //点击了第一下
//双击(第二种方式的参数)
private float endtime = 0;
private float Doubletime = 0.5f; //响应时间
public void Start(GameObject _player, Camera cam)
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
// 双击
float time = Time.realtimeSinceStartup;
if ((endtime + Doubletime) > time)
{
IsMove = false;//移动暂停
Debug.LogError("鼠标双击");
}
else
{
endtime = time;
Debug.LogError("鼠标单击");
IsMove = true;//开始移动
//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = firstCamera.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
//获取碰撞点的位置
if (hitInfo.collider.name == "Ground")
{
Debug.LogError(hitInfo.collider.name);
targetVector3 = hitInfo.point;
targetVector3.y = -1f;
IsOver = false;
}
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
}
}
if (oneClick && Time.time - firstClickTime < delay)
{
//双击
oneClick = false;
IsMove = false;//移动暂停
Debug.LogError("鼠标双击");
}
else
{
Debug.LogError("鼠标单击");
oneClick = true;
IsMove = true;//开始移动
firstClickTime = Time.time;
//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = firstCamera.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
//获取碰撞点的位置
if (hitInfo.collider.name == "Ground")
{
Debug.LogError(hitInfo.collider.name);
targetVector3 = hitInfo.point;
targetVector3.y = -1f;
IsOver = false;
}
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
}
}
}
if (IsMove)
{
//2. 让角色移动到目标位置
MoveTo(targetVector3);
}
}
//让角色移动到目标位置
private void MoveTo(Vector3 tar)
{
if (!IsOver)
{
Vector3 offSet = tar - player.transform.position;
player.transform.position += offSet.normalized * movespeed * Time.deltaTime;
if (Vector3.Distance(tar, player.transform.position) < 0.5f)
{
IsOver = true;
player.transform.position = tar;
}
}
}
unity中让物体移动到鼠标点击位置(单击移动和双击暂停移动)
标签:sep oat distance stc mesi new 方式 摄像机 time
原文地址:http://www.cnblogs.com/Study088/p/7486063.html