標籤: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中讓物體移動到滑鼠點擊位置(單擊移動和雙擊暫停移動)