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);
}
}
}