2017-08-06 22 views
2

我想爲android手機做一些控制。想要製作一個按鈕,如果玩家接觸並保持他開始向右/向左移動。如果他停下來,那邊的動作就會停止。moblie上的按鈕

我此腳本通過按住某個鍵移動:

void FixedUpdate() 
{ 
    // Add a forward force 
    rb.velocity = new Vector4(0, 0, ForwardForce * Time.deltaTime, rb.velocity.y); 

    if (Input.GetKey("d")) // If the player is pressing the "d" key 
    { 
     // Add a force to the right 
     rb.velocity = new Vector4(SidewaysForce * Time.deltaTime, 0, ForwardForce * Time.deltaTime, rb.velocity.x); 
    } 

    if (Input.GetKey("a")) // If the player is pressing the "a" key 
    { 
     // Add a force to the left 
     rb.velocity = new Vector4(-SidewaysForce * Time.deltaTime, 0, ForwardForce * Time.deltaTime, rb.velocity.y); 

    } 

回答

2

創建2個UI按鈕。

左邊1個按鈕,右邊1個按鈕。

將事件觸發器組件添加到每個按鈕。

的動作創建一個類,像這樣:

公共類PlayerMovement:MonoBehaviour {

public float speed = 3; 

public void MoveLeft(){ 
    transform.Translate(-Vector3.right * speed * Time.deltaTime); 
} 

public void MoveRight(){ 
    transform.Translate(Vector3.right * speed * Time.deltaTime); 
} 

}

調用它UpdateSelected並使用指針向上/向下的指針事件。

+0

當我調用UpdateSelected時,它會保持「按下」按鈕,以便激活腳本。基本上我不能停止運動 – JtBing

+0

使用指針向上和指針向下。 –