2014-09-03 62 views
1

我有一個左移動腳本,我想在Unity3D中使用我的精靈角色。 我想要它,只要guiTexture被按下,精靈就會移動。 下面是運動的腳本:用guiTexture移動2D角色

public float maxSpeed = 10f; 
public GameObject player; 


void Start() {} 

void FixedUpdate() { 

    float move = Input.GetAxis ("Horizontal"); 

    if (move < 0) { 
     move = move; 
    } 

    rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 

} 

回答

1

傳給你的GUI紋理玩家腳本,並命名爲「YourGuiTexture」。

有各種邏輯來檢測GuiTexture打最簡單的是下面:

在鍵盤:

public GUITexture YourGuiTexture; 

void Update() { 

    if (YourGuiTexture.HitTest(Input.mousePosition) //check if your mouse is on your gui texture 
    { 
     float move = Input.GetAxis ("Horizontal"); 

     if (move < 0) 

     { 
      move = move; 
     } 

     rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 
    } 

    } 

觸摸移動設備:

public GUITexture YourGuiTexture; 

// Update is called once per frame 
void Update() 
{ 
    if (YourGuiTexture.HitTest(Input.GetTouch(0).position)) 
    { 

     if(Input.GetTouch(0).phase==TouchPhase.Began) 
     { 
      float move = Input.GetAxis ("Horizontal"); 

      if (move < 0) 

      { 
       move = move; 
      } 

      rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); 
     }   
    } 
} 
+0

請問這仍然工作在移動設備上? – 2014-09-03 20:42:55

+0

實際上移動設備沒有鼠標連接,其中大部分工作在觸摸邏輯上我編輯了我的移動設備答案以及看看。 – 2014-09-04 06:25:29