unity中讓物體移動到滑鼠點擊位置(單擊移動和雙擊暫停移動)

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.