簡體   English   中英

在Unity中滑動時如何移動角色?

[英]How can I make my character move when slide in Unity?

我希望在滑動屏幕或觸摸手機中的Unity 3D手機中的虛擬按鈕時使角色向左或向右移動,這是玩家使用鍵盤鍵進行移動的動作腳本,但是我想適用於移動設備。

void FixedUpdate ()
{
    // Add a forward force
    rb.AddForce(0, 0, forwardForce * Time.deltaTime);

    if (Input.GetKey("d"))  // If the player is pressing the "d" key
    {
        // Add a force to the right
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (Input.GetKey("a"))  // If the player is pressing the "a" key
    {
        // Add a force to the left
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (rb.position.y < -1f)
    {
        FindObjectOfType<GameManager>().EndGame();
    }

}

解決此問題的最基本方法是(我認為):

float lastPosition;
void OnSwipte(float delta)
{
// code your movement here
}
void Update()
{
 if (Input.GetMouseButtonDown(0)) 
       lastPosition=Input.mousePosition.x; // store touch point
  else 
  if (Input.GetMouseButton(0)) 
    {
     OnSwipe(Input.mousePosition.x-lastPosition);
     lastPosition=Input.mousePosition.x; 
    }
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM